Multithreading. QTCPServer does not emit the newConnection() signal
-
I can't connect to the server side, the newConnection signal is not being emitted. If i run this in a single thread (main thread) then everything works ok.
Update:
by using netstat -a in cmd I checked that the sockets are listening for a connection and I could connect to socket via the telnet command, but after I call nentstat -a again I see that the socket to which I have connected is in two states Connnection: ESTABLISHED and Connection: LISTENING, how a socket could be in two states? and why qt does not emit the newConnection signal.void TCPSocket::run(){ while(1){ if(start_){ socket = new QTcpSocket; server = new QTcpServer; connect(server,SIGNAL(newConnection()),this,SLOT(newConnectionS())); connect(socket,SIGNAL(disconnected()),this,SLOT(Disconnected())); connect(&timer_,SIGNAL(timeout()),this,SLOT(Disconnected())); emit msg("Itializing server side..."); start_ = false; } if(server->isListening() ){ }else{ this->ListenS(); //server->waitForNewConnection(); } } }
void TCPSocket::ListenS(){ if(server->listen(QHostAddress::LocalHost,PORT_NUM)){ emit msg("Server is listening..."); qDebug()<<"Server is listening"; } else{ emit msg("Server cannot be started..."); qDebug()<<"Server cannot be started"<<server->errorString(); } }
in MainWindow ctr; for(int i=0;i<4;i++){ socketC = new TCPSocket; //connect(this,SIGNAL(startThread()),socketC,SLOT(StartThread())); //connect(socketC,SIGNAL(sendImage(QImage&)),this,SLOT(recImg(QImage&))); connect(socketC,SIGNAL(msg(QString)),this,SLOT(Log(QString))); connect(socketC,SIGNAL(users(QString)),this,SLOT(Users(QString))); connect(socketC,SIGNAL(idDisconnected(int)),SLOT(UpdateList(int))); port_struct = new PORT_; port_struct->locked = true; socketC->setPORT(DEFAULT_PORT+i); socketC->setId(i); SocketInst.push_back(socketC); PORTS_NUM.push_back(port_struct); SocketInst.at(i)->start(); }
-
Not sure if you are using the right terminology.
However, when you have QTcpServer, it is listening on specified ports. Those you should as listening. When a client connect, QTcpServer gives you socket for the communication. However, if you have not limited the number of connetions through the server, it wil continue to listen on that port.
What does the output of your application tell you?
Any warning that you are doing something you should not? -
@koahnig that's the problem the application has zero output that is why I can't understand what is the problem. When I use the netstat -a command in cmd I can see that the socket is listening for connection, and when I connect to it, the netstat -a shows that connection has been established. And there are no warnings.
The new connection function:void TCPSocket::newConnectionS(){ emit users("Test user"); emit msg("New user connected..."); qDebug()<<"Connected"; //socket->open(QIODevice::ReadWrite); socket = server->nextPendingConnection(); connect(socket,SIGNAL(readyRead()),this,SLOT(newImageReceived())); if(socket->isOpen()){ emit msg("Socket has been opened..."); qDebug()<<"Socket is open"; }else { emit msg("Socket cannot be opened..."); qDebug()<<"Socket cannot be opened"<<socket->errorString(); } }
If you want I can put the server code on a GitHub account because I really don't understand what is the problem. No errors no warnings nothing.
-
Hi,
Your thread doesn't have an event loop just an infinite blocking loop which is not a good idea in the case of QTcpServer.
If you really want to go that way, you should remove that loop and just call exec after you started your server.
I'd recommend also taking a look at the threaded version of the Fortune examples in Qt's documentation.
-
@SGaist Thank you a lot! It worked.