[Interest] Setting socket options for QWebSocketServer

Thiago Macieira thiago.macieira at intel.com
Fri May 13 23:04:57 CEST 2016


On sexta-feira, 13 de maio de 2016 20:33:53 PDT Elvis Stansvik wrote:
> 2016-05-13 19:41 GMT+02:00 Thiago Macieira <thiago.macieira at intel.com>:
> > You can't reuseaddr with TCP.
> 
> Hmm, I thought that was quite common in TCP servers? To use
> SO_REUSEADDR in order to avoid the IP/port combo remaining in the
> TIME_WAIT state for a long time, should the server closed unexpectedly
> with connected clients. I know it might not be the best practice.. but
> you mean I can't?

Ah, I see what you mean. That's not "reusing the address" in the sense that 
UDP does, in which two sockets can use the same address at the same time.

The behaviour you want makes sense. It's just that it's an abuse of the 
SO_REUSEADDR API. And sure enough, Qt is using that loophole too:

void QTcpServerPrivate::configureCreatedSocket()
{
#if defined(Q_OS_UNIX)
    // Under Unix, we want to be able to bind to the port, even if a socket on
    // the same address-port is in TIME_WAIT. Under Windows this is possible
    // anyway -- furthermore, the meaning of reusable on Windows is different:
    // it means that you can use the same address-port for multiple listening
    // sockets.
    // Don't abort though if we can't set that option. For example the socks
    // engine doesn't support that option, but that shouldn't prevent us from
    // trying to bind/listen.
    socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1);
#endif
}

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Interest mailing list