多个Socket怎么识别和下发?



  • 通过QTcpServer::nextPendingConnection()获取到多个的socket保存起来一个QList里,
    现在的问题是:要向指定的客户端发送数据,但是不知道QList<socket *>里的哪个socket对应指定的客户端?



  • 用socket描述符来区分 你想要的那个



  • @大黄老鼠 socket描述符是服务端接收到新连接,在服务端生成的?
    还是客户端生成,发送到服务端的呀?
    能否来个小小例子



  • 此回复已被删除!


  • @taadis socketserver里有个incoming参数就是socket描述符,你把它绑到一个tcpsocket上,把tcpsocket放到list中,这样,你用的时候直接从list中找你需要的



  • @大黄老鼠 文档逛了圈,没看到socketServer里有个incoming参数呀。。。



  • @大黄老鼠 还是说:一个新socket连接进来是没有标识的,连接成功后,socket再发送一个标识信息上来,然后和对应的socket绑定起来?



  • @taadis 抱歉抱歉,我没写好,我想说的是这个
    void QTcpServer::incomingConnection(qintptr socketDescriptor)
    This virtual function is called by QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.

    The base implementation creates a QTcpSocket, sets the socket descriptor and then stores the QTcpSocket in an internal list of pending connections. Finally newConnection() is emitted.

    Reimplement this function to alter the server's behavior when a connection is available.

    If this server is using QNetworkProxy then the socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket::setSocketDescriptor().

    Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.

    See also newConnection(), nextPendingConnection(), and addPendingConnection().
    你继承QTcpServer重写这个函数就好了


 

最近的回复

关注我们

微博
QQ群











召唤伊斯特瓦尔