PDA

View Full Version : QTcpServer Problem



ionutdanila
27th December 2010, 10:17
My question is simple, I suppose, but I can't figure out why this is happening.

I have a QTcpServer:



//server initialization
server= new QTcpServer();
bool success = server->listen(QHostAddress::Any, 10101);

if(!success)
{
ui->textEdit->setTextColor(Qt::red);
ui->textEdit->append(" > Could not create TopMovie Server! \n Reason: TopMovie Server is already running! \n\n This Window will automatically close!");
ui->textEdit->setTextColor(Qt::black);
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(sterge_server()));
timer->start(3000);
}
else
{
ui->textEdit->setTextColor(Qt::white);
ui->textEdit->append(" > TopMovie Server Online!");
connect(server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
ui->textEdit->setTextColor(Qt::black);
DataBase();
}

//incomingConnection
void TopMovieS::incomingConnection()
{
int Socketfd=server->nextPendingConnection()->socketDescriptor();
QTcpSocket *client = new QTcpSocket(this);
client->setSocketDescriptor(Socketfd);
clients.insert(client);

string adresa=" > New client from: "+client->peerAddress().toString().toStdString();
ui->textEdit->setTextColor(Qt::yellow);
ui->textEdit->append(adresa.c_str());
ui->textEdit->setTextColor(Qt::black);

connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(client, SIGNAL(disconnected()), this, SLOT(disconnected()));
}



I read on QTcpServer Documentation that I have to call nextPendingConnection() instead of IncommingConnection(). And also IncommingConnection() needs a QTcpSocket parameter... Anyway, this code on Windows gives a warning every time a new client is connecting: "QSocketNotifier: Multiple socket notifiers for same socket 768 and type Read", but it still works.

On Ubuntu the same code doesn't work. It gives the same error, but doesn't continue: "QSocketNotifier: Invalid socket 24 and type 'Read', disabling..."

Please help me, I am new to Qt and this is my first QTcpServer. I didn't understood everything from documentation, so the code is, I suppose, wrong.

Thank you!

stillwaiting
27th December 2010, 12:02
"QSocketNotifier: Multiple socket notifiers for same socket 768 and type Read" warning tells you that there are multiple QTcpSocket instances that use the same socket descriptor.

Instead of doing this:



int Socketfd=server->nextPendingConnection()->socketDescriptor();
QTcpSocket *client = new QTcpSocket(this);
client->setSocketDescriptor(Socketfd);


try this:



QTcpSocket *client = server->nextPendingConnection();

ionutdanila
27th December 2010, 12:18
@stillwaiting Thank you very very much! It works now!