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

Wednesday, December 28th 2011, 3:35pm

can QTcpServer incomingconnection create different threads?

Hi all!


I am taking advantage of the slot function incommingconnection(int socketDescriptor) of QTcpServer to realize a multi-thread TCP server. I tried to establish new threads in incommingconnection(int socketDescriptor) ,so all the TCP socket things would be done within the coresponding thread. And this is what I want it to be...

-----below is the incomingConnection(int socketDescriptor) of MyServer-----


Source code

1
2
3
4
5
6
7
8
9
10
11
void MyServer::icomingConnection(int socketDescriptor){ 
qDebug() << "SocketServer::incomingConnection()"; 
this->socketDescriptor = socketDescriptor; 

std::cout<<QThread::currentThread()->currentThreadId()<<" starting thread" << std::endl; 

SocketRead *listenSocket = new SocketRead(socketDescriptor,this); 
connect(listenSocket, SIGNAL(finished()), listenSocket, SLOT(deleteLater())); 
listenSocket->start(); 
listenSocket->wait(); 
}



I guess listenSocket->start() will initialize the run() function of listenSocket(below).

-----below is the run() function of SocketRead-----


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void SocketRead::run(){ 
std::cout<<QThread::currentThread()->currentThreadId()<<" starting thread" << std::endl; 
socket = new QTcpSocket(); 
qDebug() << "SocketRead::run()"; 
socket->setSocketDescriptor(socketDescriptor); 
std::cout<<"local:"<<socket->localAddress().toString().toStdString()<<":"<<socket->localPort()<<std::endl; 
std::cout<<"remote:"<<socket->peerAddress().toString().toStdString()<<":"<<socket->peerPort()<<std::endl; 

connect( socket, SIGNAL(disconnected()), this, SLOT(deleteLater()) ); 

//blocking mode 
while(!socket->waitForReadyRead(1000)); 
readPacket(); 
}



-----this is part of the output-----
(1st connection)

3086337744 starting thread
3084237712 starting thread
local:127.0.0.1:30000
remote:127.0.0.1:37828
SocketServer::incomingConnection()
SocketRead::run()
...

(2nd connection)
3086337744 starting thread
SocketServer::incomingConnection()
SocketRead::run()
3084237712 starting thread
local:127.0.0.1:30000
remote:127.0.0.1:37829

-----

However, I found it so ridiculous that all the threads created this way share the same threadID, as I displayed the threadID of each thread within the run() function. Is this a bug of Qt? Or how can I realize my original design?

This post has been edited 5 times, last edit by "laker28" (Dec 29th 2011, 2:07am) with the following reason: attaching source code and output


2

Wednesday, December 28th 2011, 5:44pm

different threads dont have same thread id
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.