QTcpServer::incomingConnection() && get ip address
-
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?
-
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
-
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...) -
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.
-
[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]
-