给你个例子自己看看把
你端口都没设呀
unit clientfrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls,winsock, ShellCtrls, StdCtrls, Buttons;
type
TFrmmain = class(TForm)
ProgressBar: TProgressBar;
opendfile: TOpenDialog;
EditIp: TEdit;
EditPort: TEdit;
BtnConnect: TBitBtn;
Btnsend: TBitBtn;
Btnstop: TBitBtn;
BtnExit: TBitBtn;
StatusBar: TStatusBar;
Label1: TLabel;
Label2: TLabel;
procedure FormCreate(Sender: TObject);
procedure BtnExitClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure BtnConnectClick(Sender: TObject);
procedure BtnsendClick(Sender: TObject);
procedure BtnstopClick(Sender: TObject);
private
client:Tsocket;
{ Private declarations }
public
stoptrans:boolean;
intrans:boolean;
procedure TransFile(filename:string);
{ Public declarations }
end;
const blocklen=1024*4;
var
Frmmain: TFrmmain;
implementation
{$R *.dfm}
procedure TFrmmain.FormCreate(Sender: TObject);
var
awsadata:TWSAData;
begin
if WSAstartup($0101,awsadata)<>0 then
raise Exception.Create('不能启动winsock动态连接库');
Messagebox(Handle,awsadata.szDescription,'winsock动态连接库版本',MB_OK);
end;
procedure TFrmmain.BtnExitClick(Sender: TObject);
begin
close;
end;
procedure TFrmmain.FormClose(Sender: TObject; var Action: TCloseAction);
var
tim:Tdatetime;
begin
if Intrans then
if Messagebox(Handle,'正在传送文件,停止吗?','提示',MB_YESNO)=IDNO then
abort;
if WSACleanup<>0 then
Messagebox(handle,'清除winsock动态连接库错误','提示',MB_OK)
else
Messagebox(Handle,'清除winsock动态连接库成功','提示',MB_OK);
closesocket(client);
end;
procedure TFrmmain.BtnConnectClick(Sender: TObject);
var
ca:SOCKADDR_IN;
hostaddr:u_long;
begin
client:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if client=INVALID_SOCKET then
begin
Statusbar.SimpleText:='为连接远程服务器创建socket错误!';
EXIT;
end;
ca.sin_family:=PF_INET;
ca.sin_port:=htons(StrtoInt(Trim(editport.Text)));
hostaddr:=inet_addr(pchar(Trim(editip.Text)));
if (hostaddr=-1) then
begin
statusbar.SimpleText:='主机ip地址:'+Trim(editip.Text)+'错误';
exit;
end
else
ca.sin_addr.S_addr:=hostaddr;
if connect(client,ca,sizeof(ca))<>0 then
begin
statusbar.SimpleText:='连接服务器端socket成功';
exit;
end
else
statusbar.SimpleText:='连接socket成功';
end;
procedure Tfrmmain.Transfile(filename:string);
var
Ftrans:file of Byte;
Flen:Integer;
BlockNum,RemainLen:Integer;
BlockBuf:array[0..BlockLen-1] of Byte;
i:Integer;
SendLen:Integer;
begin
AssignFile(Ftrans,filename);
Reset(Ftrans);
Flen:=Filesize(Ftrans);
blockNum:=flen div blocklen;
ProgressBar.Max:= 1+BlockNum;
RemainLen:=flen mod blocklen;
stoptrans:=false;
intrans:=true;
sendlen:=1;
for i:=0 to BlockNum-1 do
begin
if (stopTrans)or(sendlen<=0) then
Break;
BlockRead(Ftrans,blockbuf[0],blocklen);
sendLen:=send(client,blockbuf,blocklen,0);
progressbar.Position:=i;
Application.ProcessMessages;
end;
if stopTrans then
begin
closefile(ftrans);
Intrans:=false;
statusbar.SimpleText:='';
Messagebox(handle,'停止传输','提示',MB_OK);
Progressbar.Position:=0;
exit;
end;
if(sendlen<=0) then
begin
closefile(ftrans);
Intrans:=false;
statusbar.SimpleText:='';
Messagebox(handle,'传输异常终止!','提示',MB_OK);
progressbar.Position:=0;
exit;
end;
if Remainlen>0 then
begin
BlockRead(Ftrans,blockbuf[0],Remainlen);
sendlen:=send(client,blockbuf,Remainlen,0);
if (sendlen<=0) then
begin
closefile(ftrans);
intrans:=false;
statusbar.SimpleText:='';
Messagebox(Handle,'传输异常终止!','提示',MB_OK);
progressbar.Position:=0;
exit;
end;
end;
progressbar.Position:=progressbar.Max;
closefile(ftrans);
intrans:=false;
statusbar.SimpleText:='';
Messagebox(handle,'传输完成','提示',MB_OK);
progressbar.Position:=0;
end;
procedure TFrmmain.BtnsendClick(Sender: TObject);
begin
if (opendfile.Execute)and(fileExists(opendfile.FileName)) then
TransFile(opendfile.FileName);
end;
procedure TFrmmain.BtnstopClick(Sender: TObject);
begin
stoptrans:=true;
end;
end.
unit serverfrm;
interface
uses
Windows, Messages,winsock, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ComCtrls;
type
Tfrmmain = class(TForm)
Label1: TLabel;
otherport: TEdit;
Stabar: TStatusBar;
SaveDialog1: TSaveDialog;
btnlisten: TBitBtn;
btnreceive: TBitBtn;
btnstop: TBitBtn;
btnexit: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure btnexitClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnlistenClick(Sender: TObject);
procedure btnreceiveClick(Sender: TObject);
procedure btnstopClick(Sender: TObject);
private
{ Private declarations }
public
stoptrans:boolean;
intrans:boolean;
server:TSocket;
procedure Recefile(filename:string);
{ Public declarations }
end;
var
frmmain: Tfrmmain;
const blocklen=1024*4;
implementation
{$R *.dfm}
procedure Tfrmmain.FormCreate(Sender: TObject);
var
aWSAData:TWSAData;
begin
if WSAStartup($0101,aWSAData)<>0 then
raise Exception.Create('不能启动winsock动态连接库!');
Messagebox(Handle,awsadata.szDescription,'winsock动态连接库版本',MB_OK);
end;
procedure Tfrmmain.btnexitClick(Sender: TObject);
begin
close;
end;
procedure Tfrmmain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if intrans then
if Messagebox(handle,'正在传送文件,要停止吗?','提示',MB_YESNO)=IDNO then
abort;
if server<>INVALID_SOCKET then
closesocket(server);
if WSACleanup<>0 then
Messagebox(handle,'清除winsock动态连接库错误!','提示',MB_OK)
else
Messagebox(handle,'清除winsock动态连接库成功','提示',MB_OK);
end;
procedure Tfrmmain.btnlistenClick(Sender: TObject);
var
ca:SOCKADDR_IN;
begin
server:=Socket(PF_INET,SOCK_STREAM,IPPROTO_IP);
if server=INVALID_SOCKET then
begin
Stabar.SimpleText:='创建socket错误';
exit;
end;
ca.sin_family:=PF_INET;
ca.sin_port:=htons(strtoint(Trim(otherport.Text)));
ca.sin_addr.S_addr:=INADDR_ANY;
if bind(server,ca,sizeof(ca))=SOCKET_ERROR then
begin
stabar.SimpleText:='绑定接收端口socket错误!请更改端口!';
closesocket(server);
exit;
end
else
stabar.SimpleText:='绑定接收端socket成功!';
listen(server,5);
btnlisten.Enabled:=false;
btnstop.Enabled:=true;
end;
procedure Tfrmmain.btnreceiveClick(Sender: TObject);
begin
if (server=INVALID_SOCKET)then
begin
Messagebox(handle,'还没有进行监听,请先进行监听!','提示',MB_OK);
exit;
end;
if SaveDialog1.Execute then
Recefile(savedialog1.FileName);
end;
procedure TFrmmain.Recefile(filename:string);
var
Ftrans:file of byte;
Recelen:integer;
BlockBuf:array[0..Blocklen-1]of byte;
recvsocket:TSocket;
ra:SOCKADDR_IN;
ra_len:integer;
begin
Ra_len:=sizeof(Ra);
Recvsocket:=accept(Server,@ra,@ra_len);
AssignFile(Ftrans,filename);
ReWrite(Ftrans);
stopTrans:=false;
intrans:=true;
recelen:=recv(RecvSocket,blockBuf,blocklen,0);
while(Recelen>0)and(not stoptrans) do
begin
BlockWrite(Ftrans,BlockBuf[0],Recelen);
Application.ProcessMessages;
Recelen:=recv(RecvSocket,blockBuf,Blocklen,0);
if stopTrans then
begin
closeFile(ftrans);
closesocket(recvsocket);
intrans:=false;
Messagebox(handle,'停止传输','提示',MB_OK);
exit;
end;
end;
closefile(Ftrans);
closesocket(recvsocket);
intrans:=false;
if (recelen=SOCKET_ERROR)then
Messagebox(handle,'传输异常终止','提示',MB_OK)
else
Messagebox(handle,'客户端已经关闭连接!文件可能已经传送完毕','提示',MB_OK);
end;
procedure Tfrmmain.btnstopClick(Sender: TObject);
begin
stoptrans:=true;
if server<>INVALID_SOCKET then
closesocket(Server);
server:=INVALID_SOCKET;
btnstop.Enabled:=false;
btnlisten.Enabled:=true;
end;
end.
这是个文件传输的例子