Z
zrt_o8
Unregistered / Unconfirmed
GUEST, unregistred user!
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 = 7010; //设定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('祯听错误!');<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>//ShowMessage('连接错误.');<br>//ListBox1.Items.Add('连接错误.');<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('你好吗!');<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>小弟最近正在做一个基于GPRS手机摸块通信程序。一个服务端,多个客户端,每个客户端都有一个带TCP/IP协议的手机摸块,服务端在控制中心并接入宽带。每开启一个客户端后,服务端都能接收到客户的信息。问题是:在如何在服务端向各个客户端发送信息。这是否需要做多线程,请各位高手不吝赐教。谢谢!