Socket s = listener.AcceptSocket();然后可以将这们Socket 转交给线程处理,或都者在队列中来处理。

解决方案 »

  1.   

    是啊 ,但是我现在想要实现 向某一个socket 发送数据 ,如何处理呢 ?
      

  2.   

    帖一点代码吧,我原来做的一个通讯项目中的,缩略了些与些无关的内容:while(true)
    {

    try
    {
    Socket s = listener.AcceptSocket();
    clientsocket = s;
    clientservice = new Thread(new ThreadStart(ServiceClient));
    clientservice.Start();
    }
    catch(Exception e)
    {
    break;
    }
    }                private void ServiceClient()
    {
    Socket client = clientsocket;
    string clientcommand = ""; while (keepalive)
    {
    Byte[] buffer = new Byte[2048];
    try
    {
    datalen = client.Receive(buffer);
    }
    catch
    {

    } clientcommand += System.Text.Encoding.ASCII.GetString(buffer,0,datalen);
    if (.....)
                                    {
                                      SendToClient(client ,ref sendBuffer);
                                    }
                    }
    private void SendToClient(Client clt, string returnString)
    {
    byte[] buffer = new byte[20];
    try
    {
    buffer = System.Text.Encoding.ASCII.GetBytes(returnString.ToCharArray());
    client .Send(buffer,buffer.Length,0);
    }
    catch(Exception)
    { }
    }
      

  3.   

    每一个连接对应一个连接,要发送数据之类的处理都在特定的处理里做,都是对应着特定的Socket啊。
      

  4.   

    使用哈希表。服务器每收到一个客户端连接,就把这个连接存入哈希表。客户端socket对象做值,socket对象的ip和port组合做键,或者你找个别的什么唯一标识做键。这样就可以随时从哈希表中根据键取出值,用这个socket对象值来send数据了。
      

  5.   

    顶一下,我的方法是Socket和其他必要信息作为一个结构体放在List<>中,需要的时候循环。但是效率不高,正在探索新方法
      

  6.   

    将楼上的List<>改成Direcotry<>就好了!