QTcpServer and port problem
Hello.
I have a problem with setting up a port for QTcpServer.
Code:
void MainWindow::initServ()
{
adr.
setAddress( QString("127.0.0.1") );
adr.toIPv4Address();
quint16 port = 5050;
CRserv->setMaxPendingConnections( 10 );
if (!CRserv->listen()) {
tr("Unable to start the server: %1.")
.arg(CRserv->errorString()));
close();
return;
}else{
CRserv->listen( adr, port );
}
}
When i setup server this way i got in appliaction output window:
"QTcpServer::listen() called when already listening"
Thanx in advance for any reply.
Best regards
Re: QTcpServer and port problem
Some othere application might already be using that port. I guess Yahoo messenger uses 5050 frequently.
Try changing the port number and see if it works :)
Re: QTcpServer and port problem
It's not port at fault (i tried many combination and don't have any 'net app' running atm.)
And looking at the code that i provide its no suprise that it says that QTcpServer is runing.
To me it seams that server starts runing when i create new one
on it's default settings, ie. any adapter, random port.
When i call listen() before "if" statment i got 'qmessagebox' error with no desciption.
Any other sugestions?
Re: QTcpServer and port problem
Code:
if (!CRserv->listen()) {
tr("Unable to start the server: %1.")
.arg(CRserv->errorString()));
close();
return;
}else{
CRserv->listen( adr, port );
}
your problem is here, because if/else combination is wrong. When you use listen() and it returns true, then your server is listening on every address and any port. And if it's true then else block would be processed when you call listen() on alredy listening server with different address and port...
Re: QTcpServer and port problem
Thank you. I knew it was wrong. Without if works like a charm.
I thought that I will first check conn state, but it looks like it connect same time when i check it.
Big thanks.