QTcpServer::incomingConnection(qintptr socketDescriptor) is it possible to connect with specified socket?

928 Views Asked by At
void server::incomingConnection(qintptr socketDescriptor) {

    qDebug() << "incoming connection";
    connection* new_connection = new connection(this);
    new_connection->set_socket_descriptor(socketDescriptor);

    connect(new_connection, SIGNAL(ready_read()), this, SLOT(ready_read()));
    connect(new_connection, SIGNAL(disconnected()), this, SLOT(disconnected()));

    emit signal_new_connection(new_connection);
} 

server class is inherited from QTcpServer, and connection class has a QTcpSocket as member and some info about user who want to connect( name, ip, id...)

my problem is that i don't know nothing about new_connection. i need to know who is connecting with server. for this reason i want to connect-back but how? is there any way? or must wait till i receive data(greeting message) from connected socket(user) ?

1

There are 1 best solutions below

0
On

I've just accidentaly bumped into this old thread having the same problem. And I just found the solution, so I decided to post here in case someone has similar problem.

To get actual QTcpSocket (the one which emitted readyRead() signal), you can use QObject::sender() method, e.g.:

void NetServer::onNewConnection() {
    QObject::connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onData()));
}
// ...
void NetServer::onData() {
    QTcpSocket *client = this->server->nextPendingConnection();
    qDebug() << "Received data from" << sender();
    // or
    qDebug() << "Received data from" << this->sender();
    // or even
    qDebug() << "Received data from" << QObject::sender();
}