我举个例子,希望能有所帮助:<br><br>{--------------------------------------------------------------<br>Simple Example.<br>Implement TCP(both Client and Server) with Socket API<br><zw84611@sina.com><br>--------------------------------------------------------------}<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> WinSock, StdCtrls;<br><br>const<br> WM_SOCK = WM_USER + 1; //自定义windows消息<br> TCP_PORT = 5432; //设定TCP端口号<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> ListBox1: TListBox;<br> procedure Button1Click(Sender: TObject);<br> procedure FormClose(Sender: TObject; var Action: TCloseAction);<br> procedure FormCreate(Sender: TObject);<br> private<br> { Private declarations }<br> HasConnected, IsServer: boolean;<br> CliSocket, SvrSocket: integer;<br> SvrAddrIn, CliAddrIn:TSockAddrIn;<br> procedure InitSocket;<br> procedure SendData(Content: string);<br> procedure ReadData(var Message: TMessage); message WM_SOCK;<br> procedure SockConnect;<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.InitSocket;<br>var<br> wsadata: TWsadata;<br> err{, optval}: integer;<br>begin<br><br> WSAStartup($0101,WSAData);<br> <br> CliSocket := socket(AF_INET, SOCK_STREAM,IPPROTO_IP);<br> SvrSocket := socket(AF_INET, SOCK_STREAM,IPPROTO_IP);<br><br> if (CliSocket = INVALID_SOCKET)or(SvrSocket = INVALID_SOCKET) then<br> begin<br> ShowMessage(inttostr(WSAGetLastError())+' Socket创建失败');<br> CloseSocket(CliSocket);<br> exit;<br> end;<br><br> SvrAddrIn.sin_addr.s_addr:= INADDR_ANY;<br> SvrAddrIn.sin_family := AF_INET;<br> SvrAddrIn.sin_port :=htons(TCP_PORT);<br> Bind(SvrSocket, SvrAddrIn, sizeof(SvrAddrIn));<br><br> err := Listen(SvrSocket,5);<br> if err<>0 then ShowMessage('Listen error.');<br><br> {optval := 1;<br> if SetSockopt(SvrSocket,SOL_SOCKET,SO_REUSEADDR,pchar(@optval),sizeof(optval)) = SOCKET_ERROR then<br> begin<br> showmessage('SO_REUSEADDR set error.');<br> end; }<br><br> //绑定消息映射<br> WSAAsyncSelect(SvrSocket, Handle , WM_SOCK, FD_READ or FD_ACCEPT or FD_CONNECT or FD_WRITE or FD_CLOSE);<br> WSAAsyncSelect(CliSocket, Handle , WM_SOCK, FD_READ or FD_ACCEPT or FD_CONNECT or FD_WRITE or FD_CLOSE);<br><br>end;<br><br>procedure TForm1.SockConnect;<br>var<br> err: integer;<br>begin<br><br> CliAddrIn.sin_addr.s_addr:=inet_addr(PChar(Edit1.Text));<br> CliAddrIn.sin_family := AF_INET;<br> CliAddrIn.sin_port :=htons(TCP_PORT);<br> repeat<br> err:=connect(CliSocket,CliAddrIn, SizeOf(CliAddrIn));<br> if err = -1 then<br> begin<br> { if we use WSAAsyncSelect(CliSocket...) in order to receive data at<br> Client side, here will get error, but it still works. why?<br> }<br> //ShowMessage('connect error.');<br> //ListBox1.Items.Add('connect error.');<br> HasConnected := false;<br> break;<br> end<br> else<br> begin<br> HasConnected := true;<br> IsServer := false;<br> end;<br> until err=0;<br><br>end;<br><br>procedure TForm1.SendData(Content: string);<br>begin<br> Send(CliSocket,Content[1],length(Content),0);<br>end;<br><br>procedure TForm1.ReadData(var Message: TMessage);<br>var<br> Event: word;<br> Buf:array[0..1023] of char;<br> AddrLen, DataLen: integer;<br>begin<br> //<br> AddrLen := sizeof(SvrAddrIn);<br> Event := WSAGetSelectEvent(Message.LParam);<br><br> case Event of<br> FD_CONNECT:<br> begin<br> ListBox1.Items.Add('connect');<br> HasConnected := true;<br> //do nothing?<br> end;<br> FD_ACCEPT:<br> begin<br> IsServer := true;<br> HasConnected := true;<br> ListBox1.Items.Add('accept');<br> //CloseSocket(CliSocket);<br> CliSocket := Accept(SvrSocket,@SvrAddrIn,@AddrLen);<br> end;<br> FD_READ:<br> begin<br> DataLen := Recv(CliSocket,Buf,1024,0);<br> buf[DataLen] := #0;<br> ListBox1.Items.Add(Buf);<br> end;<br> FD_WRITE:<br> begin<br> ListBox1.Items.Add('write');<br> end;<br> FD_OOB:<br> begin<br> ListBox1.Items.Add('FD_OOB');<br> end;<br> FD_CLOSE:<br> begin<br> HasConnected := false;<br> ListBox1.Items.Add('close');<br> end;<br> end; //end of case<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> //202.104.32.230<br> if (not IsServer)and(not HasConnected) then SockConnect;<br> SendData('hello, world');<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br> CloseSocket(SvrSocket);<br> CloseSocket(CliSocket);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br> HasConnected := false;<br> IsServer := false;<br> InitSocket;<br>end;<br><br>end.<br><br><br>//源程序:http://www.playicq.com/dispdoc.php?t=27&id=2148<br>