1
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) ?

Popeye
  • 11,839
  • 9
  • 58
  • 91
Mher Didaryan
  • 95
  • 2
  • 13
  • What does "who is connecting with server" mean? I think you're missing some information in the question. – peppe Jan 30 '14 at 22:44
  • i mean,,, i need a temporary connection that will be initialized after incomingConnection function or i can initialize new_connection in incomingConnection function? (sorry for my English) – Mher Didaryan Jan 31 '14 at 09:47
  • You can, of course. But why don't you just use `nextPendingConnection()` to get a nicely connected QTcpSocket? – peppe Jan 31 '14 at 10:17
  • is there main difference between them? – Mher Didaryan Jan 31 '14 at 10:49

1 Answers1

0

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();
}
Andrew Dunai
  • 3,061
  • 19
  • 27