Home > Back-end >  Qt environment to realize the TCP communications to response the newconnect reason and solution
Qt environment to realize the TCP communications to response the newconnect reason and solution

Time:03-29

When in the client request to connect to the server, prompted:
"QAbstractSocket: : connectToHost () called the when already & up or connecting/connected to" 192.0.0.1 ""
Excuse me how should solve?
Should have test, the client receives the request has been the server end, but did not trigger the newconnect, explain the connection is not successful
Server:
 connect (tcpserver, & amp; QTcpServer: : newConnection, [=] () 
{
//remove the established connection socket
Tcpsorket=tcpserver - & gt; NextPendingConnection ();
//get the local IP address and port number
QString IP=tcpsorket - & gt; PeerAddress (), toString ();
Qint16 port=tcpsorket - & gt; PeerPort ();
QString temp=QString (" [% 1: % 2] : successful connection "). Arg (IP). Arg (port);
The UI - & gt; The read - & gt; SetText (temp);

The connect (tcpsorket, & amp; QTcpSocket: : readyRead, [=] ()
{
//read content from the communication socket
QDebug () & lt; <"Read success!" QByteArray array=tcpsorket - & gt; ReadAll ();
The UI - & gt; The read - & gt; Append (array);
});
});


Client:
 void ClientWidget: : on_buttonconnect_clicked () 
{
//get the server IP and port
QDebug () & lt; <"Start the connection!" QString IP=UI - & gt; LineEditip - & gt; The text ();
Qint16 port=UI - & gt; LineEditport - & gt; The text (). ToInt ();
Tcpsocket - & gt; ConnectToHost (QHostAddress (IP), port);
}

CodePudding user response:

Solve the building Lord, I also met this kind of question
  • Related