//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "ChaUnittMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
TWSAData aWSAData;
TSockAddrIn addr;
TIniFile *ini;
WORD w=MAKEWORD(1,1);
ini=new TIniFile(ExtractFileDir(Application->ExeName)+"\config.ini");
EdtIP->Text=ini->ReadString("Config","Server","127.0.0.1");
EdtPort->Text=IntToStr(SERVER_PORT);
EdtName->Text=ini->ReadString("Config","Name","");
EdtPort->Text=ini->ReadString("Config","Port",IntToStr(SERVER_PORT));
delete ini;
sendInfo.IsWorking=false;
recvInfo.IsWorking=false;
if(WSAStartup(w,&aWSAData)!=0)
ShowMessage("WinSock Version Error~!");
sock = socket(AF_INET,SOCK_DGRAM,0);
addr.sin_family = AF_INET;
addr.sin_port=0;
addr.sin_addr.s_addr = INADDR_ANY;
if(SOCKET_ERROR==WSAAsyncSelect(sock,MainForm->Handle,WM_SOCKETCLI,FD_READ))
ShowMessage("WM_SOCKET Error");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnConnectClick(TObject *Sender)
{
TP2PLoginPack pack;
int BufferSize = sizeof(pack)+1;
char *Buffer = new char[ BufferSize ];
Buffer[ sizeof(pack) ]='\0';
if(EdtName->Text=="")
{
ShowMessage("请输入名字");
EdtName->SetFocus();
exit(0);
}
addrSrv.sin_addr.S_un.S_addr = inet_addr((EdtIP->Text).c_str());
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(StrToInt(EdtPort->Text));
pack.head.Command=cmdLogin;
StrPCopy(pack.body.name,EdtName->Text);
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer,BufferSize,0,(LPSOCKADDR)&addrSrv,sizeof(addrSrv));
LoginTickCount=::GetTickCount();
SetTimer(MainForm->Handle,2,2000,(TIMERPROC)&OnCheckLoginResp);
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ListBox1Click(TObject *Sender)
{
TP2PUserInfoPack pack;
char *Buffer = new char[ sizeof(pack)+1 ];
Buffer[ sizeof(pack) ]='\0';
if(ListBox1->ItemIndex<0)exit(0);
pack.head.Command=cmdUserInfo;
StrPCopy(pack.body.name1,EdtName->Text);//自己
StrPCopy(pack.body.name2,(ListBox1->Items->Strings[ListBox1->ItemIndex]));//对方
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer,sizeof(pack)+1,0,(LPSOCKADDR)&addrSrv,sizeof(addrSrv));
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TimerMakeHoleTimer(TObject *Sender)
{
TP2PHolePack pack;
char *Buffer = new char[ sizeof(pack)+1 ];
Buffer[ sizeof(pack)]='\0';
pack.head.Command=cmdHole;
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer,sizeof(pack)+1,0,(LPSOCKADDR)&addrP2P,sizeof(addrP2P));
if(recvInfo.IsWorking)
{
Label5->Caption=IntToStr(recvInfo.ID-recvInfo.progress)+"KB/s";
}
if(sendInfo.IsWorking)
{
Label5->Caption=IntToStr(sendInfo.ID-sendInfo.progress)+"KB/s";
}
recvInfo.progress=recvInfo.ID;
sendInfo.progress=sendInfo.ID;
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnSendClick(TObject *Sender)
{
TP2PMessagePack pack;
char *Buffer = new char[ sizeof(pack)+1 ];
Buffer[ sizeof(pack) ]='\0';
if(EdtMessage->Text=="")return;
pack.head.Command=cmdMessage;
StrPCopy(pack.body.name,EdtName->Text);
StrPCopy(pack.body.Text,EdtMessage->Text);
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer ,sizeof(pack)+1,0,(LPSOCKADDR)&addrP2P,sizeof(addrP2P));
Memo1->Lines->Add(EdtName->Text+":"+EdtMessage->Text);
EdtMessage->Clear();
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnBrowseClick(TObject *Sender)
{
if(OpenDialog1->Execute())EdtFile->Text=OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnSendFileClick(TObject *Sender)
{
TFileStream *stream=NULL;
TP2PInquireAcceptFilePack pack;
char *Buffer = new char[ sizeof(pack)+1 ];
Buffer[ sizeof(pack) ]='\0';
if(FileExists(EdtFile->Text))
{
stream=new TFileStream(EdtFile->Text,fmOpenRead);
sendInfo.FileName=EdtFile->Text;
sendInfo.FileSize=stream->Size;
sendInfo.BlockCount=GetBlockCount(sendInfo.FileSize);//文件分为多少块
delete stream;
pack.head.Command=cmdInquireAcceptFile;
StrPCopy(pack.body.name,EdtName->Text);
StrPCopy(pack.body.FileName,ExtractFileName(EdtFile->Text));
pack.body.FileSize=sendInfo.FileSize;
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer ,sizeof(pack)+1,0,(LPSOCKADDR)&addrP2P,sizeof(addrP2P));
}
else
ShowMessage("请确认要发送的文件");
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::EdtMessageKeyPress(TObject *Sender, char &Key)
{
if(int(Key)==13)btnSendClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnClearClick(TObject *Sender)
{
Memo1->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btnRefreshClick(TObject *Sender)
{
TP2PUserListPack pack;
char *Buffer = new char[ sizeof(pack) ];
Buffer[ sizeof(pack)+1 ]='\0';
pack.head.Command=cmdUserList;
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer,sizeof(pack),0,(LPSOCKADDR)&addrSrv,sizeof(addrSrv));
delete[] Buffer;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::TimerKeepOnlineTimer(TObject *Sender)
{
TP2POnlinePack pack;
char *Buffer = new char[ sizeof(pack)+1 ];
Buffer[ sizeof(pack)]='\0';
pack.head.Command=cmdOnline;
StrPCopy(pack.body.name,EdtName->Text);
memcpy( Buffer, &pack, sizeof( pack ) );
sendto(sock,Buffer,sizeof(pack)+1,0,(LPSOCKADDR)&addrSrv,sizeof(addrSrv));
delete[] Buffer;
}
//---------------------------------------------------------------------------
int __fastcall TMainForm::GetBlockCount(int Size)
{
if(Size%BlockSize==0)
{
return Size/BlockSize;
}
else
{
return (Size/BlockSize)+1;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::WMSOCKETCLI(Messages::TMessage& msg)
{
TSockAddrIn addr;//接收到的主机信息
int addrlen;//地址结构长度
int i;//for循环专用
byte buffer[1500];//数据包
TP2PHead head;//包头
//i开头的是接收的消息结构,o开头的是发出的消息结构
TP2PLoginResp iP2PLoginResp;//server登录回应
TP2PUserListPack oP2PUserListPack;//请求用户列表
TStringList *users;//在线用户信息临时变量,用于分割字符
TP2PUserListResp iP2PUserListResp;//server返回的在线用户信息
TP2PUserInfoResp iP2PUserInfoResp;//server返回的指定用户信息
TP2PMakeHole iP2PMakeHole;//server要求你打洞,包含对方的主机信息
TP2PMessage iP2PMessage;//p2p之间的聊天信息
TP2PInquireAcceptFile iP2PInquireAcceptFile;//询问是否接收文件
TP2PInquireAcceptFileResp iP2PInquireAcceptFileResp;//同上,只是方向不同
PChar fileBuffer;//用来预先分配硬盘空间,来创建一个文件
TP2
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
p2p聊天示范.rar (53个子文件)
p2p聊天示范
p2p_client
Project1.obj 17KB
config.ini 57B
ChaUnittMain.~ddp 51B
Unit1.obj 41KB
ChaUnittMain.h 3KB
Project1.exe 388KB
ChaUnittMain.~h 3KB
ChatproMain.~cpp 905B
ChaUnittMain.ddp 51B
ChatproMain.obj 17KB
ChatproMain.tds 2.63MB
ChatproMain.res 876B
ChaUnittMain.obj 118KB
ChatproMain.exe 587KB
ChaUnittMain.dfm 6KB
res
msg.wav 9KB
ring.wav 15KB
ChatproMain.~bpr 7KB
ChaUnittMain.~dfm 6KB
ChaUnittMain.~cpp 23KB
ChatproMain.cpp 905B
ChatproMain.bpr 7KB
ChaUnittMain.cpp 23KB
p2p_clientconfig.ini 56B
p2p_server
SeverMainForm.ddp 51B
SeverMainForm.dfm 1KB
Common.~h 4KB
Unit1.obj 121KB
SeverMainPro.~cpp 898B
Unit1.dfm 1KB
Common.cpp 238B
SeverMainPro.~bpr 7KB
SeverMainPro.bpr 7KB
Unit1.~cpp 9KB
Common.~cpp 238B
Unit1.~dfm 1KB
Common.obj 15KB
SeverMainPro.str 0B
SeverMainForm.~ddp 51B
server.ini 19B
SeverMainPro.cpp 898B
SeverMainPro.obj 17KB
SeverMainPro.exe 545KB
Unit1.h 1KB
SeverMainPro.res 876B
Unit1.ddp 51B
Unit1.~ddp 51B
Common.h 4KB
SeverMainForm.~dfm 1KB
Unit1.cpp 9KB
Unit1.~h 1KB
Bin
config.ini 57B
server.ini 19B
共 53 条
- 1
资源评论
- yyy5012013-01-31不错!能够正常运行,通讯没有问题。 正是我需呀找到,服务端和客户端都有,可以学的东西很多!谢谢分享
- imisu2011-11-12开始以为是delphi源码 下载了才知道是c++ builder的 但是运行了 外网服务器能够检测到客户端登陆,但是聊天没反应
kesaly
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功