多个Socket怎么识别和下发?



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



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



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



  • This post is deleted!


  • @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重写这个函数就好了


Log in to reply
 

走马观花

最近的回复

  • 自己顶顶有益健康

    read more
  • @qyvlik 这个例子可否提供学习一下?

    read more
  • @zhoujin7 这个是PySide的代码。的确有深度……
    还要配置环境才可以顺利地跑通你的代码呢。

    read more
  • 这个可以自由拖动, 但是不能自动对齐, 不是我需要的效果

    read more

关注我们

微博
QQ群