QTcpServer::incomingConnection() && get ip address
-
wrote on 15 Nov 2010, 15:22 last edited by
Hi all,
I have a QTcpServer subclass:
@
class StarServer : public QTcpServer
{
Q_OBJECT
public:
StarServer(int port, QObject *parent = 0);
protected:
void incomingConnection(int socketDescriptor);
...
}
@incomingConnection create a QThread and pass it the socketDescriptor:
@
void StarServer::incomingConnection(int socket_descriptor)
{
AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(socket_descriptor, this);
thread->start();
}
@I need to know the ip address of the remote host that connect to the server before starting the tread using socket_descriptor.
I tried this:
@
void StarServer::incomingConnection(int socket_descriptor)
{
QTcpSocket tcp_sock_tmp;
tcp_sock_tmp.setSocketDescriptor(socket_descriptor);
qDebug() << tcp_sock_tmp.peerAddress().toString();AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(socket_descriptor, this); thread->start();
}
@In this way I get the ip address but next the thread can't use the socket_descriptor.
Is there another way to get the ip?
-
wrote on 15 Nov 2010, 15:26 last edited by
Hi,
you should connect to newConnection() signal, and then get the new QTcpSocket from nextPendingConnection().
So you have localAddress(), localPort(), peerAddress() and peerPort(), I mean, everything you need :)
Tony.
-
wrote on 15 Nov 2010, 15:31 last edited by
I also need to pass the socketDescriptor to a thread so I must use StarServer::incomingConnection() .
-
wrote on 15 Nov 2010, 15:36 last edited by
Well, why don't you pass the QTcpSocket pointer to the thread? Do you need to customize some socket options?
T.
-
wrote on 15 Nov 2010, 15:43 last edited by
Using QTcpSocket in a different thread require to pass the socket descriptor to the thread and construct the QTcpSocket in the QThread subclass as shown in the "threaded fortune server example":http://doc.qt.nokia.com/latest/network-threadedfortuneserver.html
-
wrote on 15 Nov 2010, 15:49 last edited by
I see ... well, I used to pass QTcpSocket and make a "moveToThread", to fix thread affinity.
T.
-
wrote on 15 Nov 2010, 16:12 last edited by
It works! Thanks!
I've never tried to construct a QTcpSocket and then move it to another thread.Now I have this:
@
void StarServer::incomingConnection(int socket_descriptor)
{
QTcpSocket *tcp_sock_tmp = new QTcpSocket();
tcp_sock_tmp->setSocketDescriptor(socket_descriptor);
qDebug() << tcp_sock_tmp->peerAddress().toString();AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(tcp_sock_tmp, this); thread->start();
}
@and in AutenticazioneSatelliteThread :
@
AutenticazioneSatelliteThread::AutenticazioneSatelliteThread(QTcpSocket *socket, QObject *parent)
: QThread(parent)
{
qDebug() << socket->peerAddress().toString();
socket->moveToThread(this);autenticazioneSatelliteSocket = socket;
....
@Thanks again!
(se capiti in Ancona... ti devo una birra...) -
wrote on 15 Nov 2010, 17:13 last edited by
You're welcome!
( ci conto :) )
-
wrote on 29 May 2011, 02:05 last edited by
Hi, are you sure that the client socket is running in a seperate thread after using
@socket->moveToThread(this); @ in the constructor of the thread class ?I think that the thread class won't execute in the second thread until the "run" is called, which implys that the constructor of the thread class executes still in the main thread. It probably indicates that your "moveToThread" won't move the socket to the second thread. You socket client will execute still in the main thread.
-
wrote on 30 May 2011, 09:04 last edited by
[quote author="Luca" date="1289835800"]Using QTcpSocket in a different thread require to pass the socket descriptor to the thread and construct the QTcpSocket in the QThread subclass as shown in the "threaded fortune server example":http://doc.qt.nokia.com/latest/network-threadedfortuneserver.html
[/quote]BTW, I asked and I've been told that the docs (and the example) are wrong about that point:
[quote]Note: The returned QTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override incomingConnection().[/quote]
-