PDA

View Full Version : how to make tcp server listen to a specific port using QTcpServer



anurupr
19th March 2010, 20:55
i tried using QHostAddress to initialse my QTcpServer object but whenever i start the server i get an error unable to start the server.
this is the code for initialising the server



QTcpServer *tcpserver;
tcpserver = new QTcpServer(this);

QHostAddress hostadd("192.168.3.89");

tcpserver->listen(hostadd,23456);
if (!tcpserver->listen()) {

QMessageBox::critical(this, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpserver->errorString()));
close();
return;
}



when i compile i get no error.
is this correct??? please help me
thanx in advance

JohannesMunk
21st March 2010, 21:14
Shoudn't it be:



QTcpServer *tcpserver;
tcpserver = new QTcpServer(this);

QHostAddress hostadd("192.168.3.89");

if (!tcpserver->listen(hostadd,23456)) {
QMessageBox::critical(this, tr("Fortune Server"), tr("Unable to start the server: %1.").arg(tcpserver->errorString()));
close();
return;
}

You were checking the return value of the wrong call to listen..

HIH

Johannes