PDA

View Full Version : QTcpServer and port problem



Talei
28th April 2009, 00:08
Hello.
I have a problem with setting up a port for QTcpServer.



void MainWindow::initServ()
{
QHostAddress adr;
adr.setAddress( QString("127.0.0.1") );
adr.toIPv4Address();
quint16 port = 5050;

QTcpServer *CRserv = new QTcpServer();
CRserv->setMaxPendingConnections( 10 );
CRserv->setProxy(QNetworkProxy::NoProxy);

if (!CRserv->listen()) {
QMessageBox::critical(this, tr("CR_bypasser"),
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

aamer4yu
28th April 2009, 05:15
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 :)

Talei
28th April 2009, 06:30
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

QTcpServer *CRserv = new QTcpServer();
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?

faldzip
28th April 2009, 07:18
if (!CRserv->listen()) {
QMessageBox::critical(this, tr("CR_bypasser"),
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...

Talei
28th April 2009, 18:01
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.