关于socket的列表和委托的问题
程序代码:
public List<Socket> ClientSocket = new List<Socket>();//为客户端建立的SOCKET连接 //服务器侦听线程中新建客户端连接的方法 private void RecieveAccept() { while (true) { Socket NewSocket = ServerSocket.Accept(); NewSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(RecieveCallBack), NewSocket); ClientSocket.Add(NewSocket); } } //处理客户端发送的数据回发数据给客户端 private void RecieveCallBack(IAsyncResult AR) { Socket RSocket = (Socket)AR.AsyncState; int REnd = RSocket.EndReceive(AR); RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket); }
代码就是上面的了,问题是,每次接受到数据后,我不知道是ClientSocket 里具体哪个Socket发来的消息,土一点的办法是接受到消息之后去列表里循环找IP匹配的项,但这样会不会太挫……