You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, December 27th 2010, 10:23am

QTcpServer Problem

My question is simple, I suppose, but I can't figure out why this is happening.

I have a QTcpServer:


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 //server initialization void TopMovieS::initializare()
 {
     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();
     }
 }

//new connectionvoid 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!

dipindp85

Beginner

  • "dipindp85" is male

Posts: 4

Location: TRIVANDRUM

Occupation: IT PROFESSIONAL

  • Send private message

2

Monday, December 27th 2010, 12:35pm

I don't know why you are assigning the pending TCPServer's descriptor to the TCPSocket. There is another easy method to assign the pending connection to tcpsocket. Please try with ,


QTcpSocket *client=server->nextPendingConnection();


Hope you have created the object for server and create the signal slot connections of the client just below this line with out using the &operator. This will solve the issue.



Regards,

Dipin 8)