Home > other >  Ask a question at the same time use QThread and QSocket
Ask a question at the same time use QThread and QSocket

Time:10-05

My university you write socket communication at that time, the teacher asked me to use useless to multithreading, I was barely realize communication and interface effect, work for a year, also want to rewrite the program, multi-threaded network communication has always been in my heart a camp,

I searched on the Internet, I found that with the two methods can realize communication,

 
Void Server: : incomingConnection (qintptr handle)
{
//(1)
//SocketThread * socket_thread=new SocketThread (handle);
//socket_thread - & gt; Start ().

//(2)
ServerTcpSocket * server_socket=new ServerTcpSocket (handle);
Server_socket - & gt; MoveToThread (new QThread ());
AddPendingConnection (server_socket);
}


The first is a server in the new connection, create a thread, thread has a socket, thread: : run () as follows,
 
Void SocketThread: : run ()
{
Socket ServerTcpSocket *=new ServerTcpSocket (ptr_);
The socket - & gt; WaitForReadyRead ();
The exec ();
}


The second is the server in the new connection, create a socket, if not add server_socket - & gt; MoveToThread (new QThread ()); This line of code, the socket is the client can read the news, plus moveToThread () cannot do,


Why the two are not?

CodePudding user response:

Original second moveToThread the thread didn't start () (), it can run, I want to use the first idea, thread: : run () specific to how to write can be received the socket when debugging: : readyRead ()
  • Related