winsock多客户端问题(100分)

  • 主题发起人 主题发起人 zrt_o8
  • 开始时间 开始时间
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&lt;&gt;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协议的手机摸块,服务端在控制中心并接入宽带。每开启一个客户端后,服务端都能接收到客户的信息。问题是:在如何在服务端向各个客户端发送信息。这是否需要做多线程,请各位高手不吝赐教。谢谢!
 
咋没有人来拿分啊
 
这么长的啊,呵呵,不懂。<br>procedure SendTextToClient(Const ASocket: TServerSocket; Const AText: String);<br>var<br> &nbsp;j: Integer;<br>begin<br> &nbsp;for j:=0 to ASocket.Socket.ActiveConnections-1 do<br> &nbsp;try<br> &nbsp; &nbsp;ASocket.Socket.Connections[j].SendText(AText);<br> &nbsp;except<br> &nbsp;end;<br>end;<br><br>我用的是ServerSocket/ClientSocket,不知是否有所启发。
 
关注!客户端的数量凭什么来确定,它的上限又是多少呢?有什么软件可以来检测?
 
如果服务端和客户端的通信量不大的话,而且并发在一百之内的话,用你上面的异步SELECT模型就可以了。<br>如果是长时间的连接和较大的通信量,还是走多线程的服务为好。
 
楼主,你有没有异步这方面的资料啊!<br>能不能发给我一份!?kp1150@163.com
 
可以把客户端的信息包括SOCKET放入LIST中
 
建立监听线程,接收到连接后,读取客户端信息,一并和accept生成的socket作为对象存入<br>list
 
直接使用API来做,不难!<br>只要是在客户端连接上来的时候将套接字记录下来,然后发送的时候使用这个套接字将数据发送出去就可以了!<br>如果有上千的客户端连接建议使用IOCP方式!
 
用tcpclient控件不就行了!
 
用IPCP在www.tomore.com上有例子,看去吧
 
http://www.delphibbs.com/keylife/images/u196832/reverse0617.rar
 
后退
顶部