[Interest] QTcpServer. Connecting.Sending data.
Tony Rietwyk
tony at rightsoft.com.au
Thu Oct 10 01:42:45 CEST 2013
Hi Alexander,
QTcpServer.listen only sets up the listening - it does not wait until a new
connection arrives. In my code, I only call listen once, then run an event
loop. The new connections arrive in the incomingConnection slot.
void MyThread::run()
{
srv = new (std::nothrow) tcp_server(ised_win_raw_ptr);
if (!connect(srv,SIGNAL(newConnection(),srv,SLOT(connection())
)) error....
if (!srv->listen(QHostAddress::Any, port)) error....
exec();
srv->close();
delete srv;
}
Of course, you can put the slot anywhere. The slot uses
nextPendingConnection() to get the socket.
Works well for me.
If you cannot use QThread.exec, then use srv->waitForNewConnection in your
terminate loop.
Tony
From: interest-bounces+tony=rightsoft.com.au at qt-project.org
[mailto:interest-bounces+tony=rightsoft.com.au at qt-project.org] On Behalf Of
Alexander Syvak
Sent: Thursday, 10 October 2013 5:11 AM
To: Qt Interest
Subject: [Interest] QTcpServer. Connecting.Sending data.
Hello,
here's the code of the redefined method run() of QThread class =>
void run() override
{
srv = new (std::nothrow) tcp_server(ised_win_raw_ptr);
#ifdef DEBUG
qDebug() << "listener's thread => " << thread();
#endif
terminate_ = false;
while ( !terminate_ ) if ( !srv->isListening() )
srv->listen(QHostAddress::Any, port);
#ifdef DEBUG
qDebug() << "Deleting the ised_tcp_server object.";
#endif
srv->close(); // Closes the server. The server will no longer listen
for incoming connections.
delete srv;
}
The program connects to 9000 port for listening.
Using the netcat command
nc -vv 127.0.0.1 9000
yields
PC_NAME [127.0.0.1] 9000 (?) open
Then I typed start and hit Enter.
The break point has been set in incomingConnection which is redefined
in the tcp_server class inheriting QTcpServer.
Debugger did not paused in the method body when netcat connected to the
port.
Solutions?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20131010/3b53ce42/attachment.html>
More information about the Interest
mailing list