一个服务端和若干个客户端 
当服务端和若干客户端建立连接后
服务端如何给特定的客户端发送消息?自己的想法是再建立连接时 给每个客户端添加标识大家都是怎么解决的??

解决方案 »

  1.   

    晕,这个是一个基本的问题啊;
    服务器接收连接(accept)以后,
    会有一个socket套接字还有客户端的IP及端口信息,
    这些足以标识一个socket了。
      

  2.   

    楼上的麻烦再详细点
    socke[i]=accept();
    我如何得到socket[i]的端口和ip那?
      

  3.   

    你看看MSDN上面说的很清楚
    accept
    The Windows Sockets accept function accepts an incoming connection attempt on a socket.SOCKET accept (
      SOCKET s,                   
      struct sockaddr FAR* addr,  
      int FAR* addrlen            
    );
     
    Parameters

    [in] A descriptor identifying a socket that has been placed in a listening state with the listen function. The connection will actually be made with the socket that is returned by accept. 
    addr 
    [out] An optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family established when the socket was created. 
    addrlen 
    [out] An optional pointer to an integer that contains the length of the address addr. struct sockaddr_in{
        short            sin_family;
        unsigned short      sin_port;  // 客户端端口
        struct   in_addr      sin_addr; // 客户端地址
        char               sin_zero[8];
    };【Return Values】
    If no error occurs, accept returns a value of type SOCKET that is a descriptor for the new socket. This returned value is a handle for the socket on which the actual connection will be made.Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.The integer referred to by addrlen initially contains the amount of space pointed to by addr. On return it will contain the actual length in bytes of the address returned.
      

  4.   

    accept返回值就是连接标示socket