我把源码给你,客户端unit Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls,WinSock, XPMan;type TFrmMain = class(TForm) OpenDFile: TOpenDialog; Progressbar1: TProgressBar; BitBegin: TBitBtn; BitStop: TBitBtn; BitExit: TBitBtn; Label1: TLabel; Label2: TLabel; StaBar: TStatusBar; Edit1: TEdit; Edit2: TEdit; XPManifest1: TXPManifest; procedure BitExitClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BitBeginClick(Sender: TObject); procedure BitStopClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans :Boolean; InTrans :Boolean; procedure CoveyFile(filename:String); end;var FrmMain: TFrmMain;implementation{$R *.DFM}procedure TFrmMain.BitExitClick(Sender: TObject);begin Close;end;procedure TFrmMain.FormCreate(Sender: TObject);var aWSAData : TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then begin MessageBox(Handle,'不能启动winsock动态链接库!','错误',MB_OK); Exit; end;end;procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);var tim : Tdatetime;begin if InTrans then if MessageBox(Handle,'正在传输文件,停止吗?','提示',MB_YESNO)=IDNO then begin Action := caNone; Exit; end else begin StopTrans := True; tim := Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; end; if WSACleanup<>0 then MessageBox(Handle,'清除WinSock动态链接库错误!','提示',MB_OK)end;procedure TFrmMain.CoveyFile(filename:String);Const BlockLen=1024*4;var Fby : File of Byte; FL : Integer; BlNum,ReLen : Integer; BlBuf : Array[0..BlockLen-1] of Byte; i : Integer; SL : Integer; Client : TSocket; ca : SOCKADDR_IN; Haddr : u_long;begin Client := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if Client=INVALID_SOCKET then begin StaBar.SimpleText := '为连接远程服务器端创建Socket错误!' ; Exit; end; ca.sin_family := PF_INET; ca.sin_port := htons(StrToInt(Trim(Edit2.Text))); //获取端口 Haddr := inet_addr(PChar(Trim(Edit1.Text))); //获取IP地址 if (Haddr=-1) then begin StaBar.SimpleText := ' 主机IP地址 :'+Trim(Edit1.Text)+' 错误!' ; closesocket(Client); Exit; end else ca.sin_addr.S_addr := Haddr; if connect(Client,ca,sizeof(ca))<>0 then begin StaBar.SimpleText := '服务器连接错误!'; closesocket(Client); Exit; end else begin StaBar.SimpleText := '远程连接成功!' ; Progressbar1.Visible := True; end; AssignFile(Fby,filename); Reset(Fby); FL := FileSize(Fby); BlNum := FL div BlockLen; Progressbar1.Max := 1+BlNum; ReLen := FL mod BlockLen; StopTrans := False; InTrans := True; SL := 1; for i:=0 to BlNum-1 do begin if (StopTrans)or(SL<=0) then Break; BlockRead(Fby,BlBuf[0],BlockLen); SL := send(Client,BlBuf,BlockLen,0); Progressbar1.Position := i; Application.ProcessMessages; end; if StopTrans then begin CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'停止传输!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; if (SL<=0) then begin CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'传输异常终止!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; if ReLen>0 then begin BlockRead(Fby,BlBuf[0],ReLen); SL := send(Client,BlBuf,ReLen,0); if (SL<=0) then begin CloseFile(Fby); closesocket(Client); InTrans:=False; StaBar.SimpleText := ''; MessageBox(Handle,'传输异常终止!','提示',MB_OK); Progressbar1.Position := 0; Exit; end; end; Progressbar1.Position := Progressbar1.Max; CloseFile(Fby); closesocket(Client); InTrans := False; StaBar.SimpleText := ''; MessageBox(Handle,'传输完成!','提示',MB_OK); Progressbar1.Position := 0;end;procedure TFrmMain.BitBeginClick(Sender: TObject);begin if (OpenDfile.Execute)and (FileExists(OpenDfile.FileName)) then CoveyFile(OpenDfile.FileName);end;procedure TFrmMain.BitStopClick(Sender: TObject);begin StopTrans := True;end;end.服务器端unit Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Buttons, ComCtrls,WinSock, XPMan;type TFrmMain = class(TForm) BitBegin: TBitBtn; BitStop: TBitBtn; BitExit: TBitBtn; Label2: TLabel; StatusBar1: TStatusBar; BitListen: TBitBtn; Edit2: TEdit; XPManifest1: TXPManifest; SaveDialog1: TSaveDialog; procedure BitExitClick(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure BitBeginClick(Sender: TObject); procedure BitStopClick(Sender: TObject); procedure BitListenClick(Sender: TObject); private { Private declarations } public { Public declarations } StopTrans :Boolean; InTrans :Boolean; Server :TSocket; procedure MeetFile(filename:String); end;var FrmMain: TFrmMain;implementation{$R *.DFM}procedure TFrmMain.BitExitClick(Sender: TObject);begin Close;end;procedure TFrmMain.FormCreate(Sender: TObject);var aWSAData:TWSAData;begin if WSAStartup($0101,aWSAData)<>0 then begin MessageBox(Handle,'不能启动WinSock动态链接库!','错误',MB_OK); Exit; end;end;procedure TFrmMain.FormClose(Sender: TObject; var Action: TCloseAction);var tim:Tdatetime;begin if InTrans then if MessageBox(Handle,'正在传输文件,是否退出?','提示',MB_YESNO)=IDNO then begin Action:=caNone; Exit; end else begin StopTrans:=True; tim:=Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; end; if Server<>INVALID_SOCKET then closesocket(Server); if WSACleanup<>0 then MessageBox(Handle,'清除WinSock动态链接库错误!','提示',MB_OK)end;procedure TFrmMain.MeetFile(filename:String);Const BlockLen=1024*4;var Fby : File of Byte; ReL : Integer; Buf : Array[0..BlockLen-1] of Byte; Rket : TSocket; ra : SOCKADDR_IN; ralen : Integer;begin ralen := sizeof(Ra); Rket:=accept(Server,@ra,@ralen); AssignFile(Fby,filename); ReWrite(Fby); StopTrans := False; InTrans := True; ReL := recv(Rket,Buf,BlockLen,0); while (ReL>0) and (not StopTrans) do begin BlockWrite(Fby,Buf[0],ReL); Application.ProcessMessages; ReL := recv(Rket,Buf,BlockLen,0); end; if StopTrans then begin CloseFile(Fby); closesocket(Rket); InTrans := False; MessageBox(Handle,'停止传输!','提示',MB_OK); Exit; end; CloseFile(Fby); closesocket(Rket); InTrans := False; if (ReL=SOCKET_ERROR) then MessageBox(Handle,'传输异常终止!','提示',MB_OK)end;procedure TFrmMain.BitBeginClick(Sender: TObject);begin if (Server=INVALID_SOCKET) then begin MessageBox(Handle,'请先进行监听!','提示',MB_OK); Exit; end; if SaveDialog1.Execute then MeetFile(SaveDialog1.FileName);end;procedure TFrmMain.BitStopClick(Sender: TObject);var tim:Tdatetime;begin StopTrans := True; tim := Now; while (Now-tim<0.0001) and InTrans do AppliCation.ProcessMessages; if Server<>INVALID_SOCKET then closesocket(Server); Server := INVALID_SOCKET; BitStop.Enabled := False; BitListen.Enabled := True;end;procedure TFrmMain.BitListenClick(Sender: TObject);var ca : SOCKADDR_IN;begin Server := Socket(PF_INET, SOCK_STREAM, IPPROTO_IP); if Server=INVALID_SOCKET then begin StatusBar1.SimpleText := '创建接收错误!' ; Exit; end; ca.sin_family := PF_INET; ca.sin_port := htons(StrToInt(Trim(Edit2.Text))); ca.sin_addr.S_addr := INADDR_ANY; if bind(Server,ca,sizeof(ca))=SOCKET_ERROR then begin StatusBar1.SimpleText := '绑定接收端错误!请更改接收端口!'; closesocket(Server); Exit; end else StatusBar1.SimpleText := '绑定接收端成功!'; listen(Server,5); BitListen.Enabled := False; BitStop.Enabled := True;end;end.