You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Saturday, September 7th 2013, 10:51am

Why would QTcpSocket have empty peerAddress and peerPort?

I have a server application which uses QTcpServer. One class is for connection with SSL and another without SSL.
I have overloaded acceptConnection(qintptr) and most of the time it works as it's supposed to. However, there are many cases where the peerPort is 0 and peerAddress is null. I would think that the client has disconnected before I called those functions to get the data, but there are many entries in my log for that case, and I started to wonder if something is actually wrong. Here's my implementation:


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void TcpServer::acceptConnection(qintptr socketDescriptor)
{
    QTcpSocket *sock = new QTcpSocket(this);


    if (!sock->setSocketDescriptor(socketDescriptor))
    {
        LogError(QString("setSocketDescriptor failed! Error: %1. SD: %2.").arg(sock->errorString()).arg(socketDescriptor));
        delete sock;
        return;
    }


    if (!sock->peerPort() || sock->peerAddress().isNull())
    {
        LogError(QString("Empty peer data."));
        delete sock;
        return;
    }


    connect(sock, SIGNAL(readyRead()), this, SLOT(dataReceived()));
    connect(sock, SIGNAL(disconnected()), this, SLOT(socketDisconnected()));
    connect(sock, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten(qint64)));
    connect(sock, SIGNAL(error(QAbstractSocket::SocketError)), this,  SLOT(socketError(QAbstractSocket::SocketError)));
    
    LogInfo(QString("Incoming connection. Addr: %1. Port: %2.").arg(sock->peerAddress().toString()).arg(sock->peerPort()));
    emit connectionAccepted(sock->peerAddress(), sock->peerPort());
}




I have never seen "setSocketDescriptor failed" in my log. However, there are many entries that say "Empty peer data".
If it's of any help, I am running it on Ubuntu 13.04 x64.

If you have any suggestions, please let me know.