procedure TForm1.Button1Click(Sender: TObject);
begin
cs.Address:=edit1.Text;
cs.Port:=2000;
cs.Active:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Begin
cs.Socket.SendText(MP_QUERY+OpenDialog1.FileName);//FileSize???
end;
end;
procedure TForm1.csRead(Sender: TObject; Socket: TCustomWinSocket);
var
sTemp,srecv,MP_FILEPROPERTY:string;
bufSend:pointer;
fssend:tfilestream;
iBYTEPERSEND:integer;
begin
mp_fileproperty:= '发送开始:';
iBYTEPERSEND:=2048;
sRecv:=Socket.ReceiveText;
Case sRecv[1] of
MP_REFUSE:
ShowMessage('Faint,be refused!');
MP_ACCEPT:
begin
fsSend:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
//iBYTEPERSEND是个常量,每次发送包的大小。
Socket.SendText(MP_FILEPROPERTY+inttostr(Trunc(fsSend.Size/iBYTEPERSEND)+1));
end;
MP_NEXTWILLBEDATA:
begin
Socket.SendText(MP_NEXTWILLBEDATA);
end;
MP_DATA:
begin
try
GetMem(bufSend,iBYTEPERSEND+1);
if (fsSend.Position+1+iBYTEPERSEND) < fsSend.Size then
begin
fsSend.Read(bufSend^,iBYTEPERSEND);
Socket.SendBuf(bufSend^,iBYTEPERSEND);
fsSend.Free;
end//普通的发送,大小为iBYTEPERSEND
else
begin
fsSend.Read(bufSend^,fsSend.Size-fsSend.Position-1);
Socket.SendBuf(bufSend^,fsSend.Size-fsSend.Position-1);
end;//最后一次发送,发送剩余的数据
finally
FreeMem(bufSend,iBYTEPERSEND+1);
end;{of try}
end;
MP_ABORT:
begin
//被取消了:(
fsSend.Free;
end;
end;{of case}
//用Delphi编写点对点传文件程序(2)
end;
服务器端:
procedure TForm1.Button1Click(Sender: TObject);
begin
ss.Port:=2000;
ss.Open;
end;
procedure TForm1.ssClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
stemp:string;
bufrecv:pointer;
irecvlength:integer;
fsrecv:tfilestream;
begin
breadtext:=true;
if breadtext then
// if 1=1 then
begin
sTemp:=Socket.ReceiveText;
case sTemp[1] of
MP_QUERY:
begin
//在这里拒绝
SaveDialog1.FileName:=Copy(sTemp,2,Length(STemp)-1);
if SaveDialog1.Execute then
begin
Socket.SendText(MP_ACCEPT);
MessageBox( Handle, '建立前。', '提示信息',
MB_OK + MB_ICONEXCLAMATION + MB_DEFBUTTON1 );