How to write QT5 chat room (a) using a TCP protocol TCP of a server write

Knowledge about the TCP protocol

TCP protocol is a protocol based on the transport layer, with reliability and must be connected, full-duplex mode of operation, the transmission speed is slower compared to the UPD characteristics, typically for transmitting large amounts of data, transmission loss is not allowed situation. in general chat type of software are using UDP protocol, TCP is used here in order to understand the characteristics of TCP, as well as to ensure the process does not affect the experimental results because of packet loss.

Experimental ideas

I will be on the server side is divided into three parts, each part separately for different functions.

Part I: server shape design, the server should display a dialog client login and logout information is transmitted during a sign, a text box displays a port number, a button to turn on the server

Second part: the server capability, the server listens to a fixed port, when there is access to the client port, creates a TCP socket for the client object to the service (in order to communicate with the client at the server), the server information needed can be stored for each access client

Part III: a TCP socket object function, the object needs to be able to read the data in the socket, which is transmitted to the server,

Implementation

The following code in the .cpp part of the code, the code reference << Qt5 development and examples (third edition) >> If there is error correction understanding would like to present

first part:

. 1 tcpserver tcpserver :: (the QWidget * parent, the Qt :: WindowFlags F) // Constructor 
2      : QDialog (parent, F)
 . 3  {
 . 4      setWindowTitle (TR ( " the TCP Server " )); // change the name 
. 5      ContentListWidget = new new QListWidget; // initialize a dialog box displaying the appropriate information 
. 6      PortLabel = new new the QLabel (TR ( " port " )); initialize a label, allowed to display the corresponding content
 . 7      PortLineEdit = new new QLineEdit; // initialize a dialog box is displayed fixed port number 
. 8      CreateBtn = new newThe QPushButton (TR ( " Create Chat Room " )); // initialize a button, let display the corresponding content 
. 9      mainLayout = new new QGridLayout ( the this ); // initialize a QGridLayout layout, and the layout of all components into 
10      mainLayout -> addWidget (ContentListWidget, 0 , 0 , . 1 , 2 );
 . 11      mainLayout-> addWidget (PortLabel, . 1 , 0 );
 12 is      mainLayout-> addWidget (PortLineEdit, . 1 , . 1 );
 13 is      mainLayout-> addWidget (CreateBtn, 2 ,0 , . 1 , 2 );
 14      Port = 8010 ; // listening port 8010 
15      PortLineEdit-> the setText (QString :: Number (Port)); // port number display 
16      Connect (CreateBtn, the SIGNAL (clicked ()), the this , the sLOT (slotCreateServer ())); // press the button sends a signal to trigger slot function, the function-groove to create a server 
. 17  }
 18 is tcpserver tcpserver :: ~ () // destructor 
19  {
 20  }
 21  void tcpserver :: slotCreateServer () // realize functions groove 
22 is  {
 23 is      Server = new newServer ( the this , Port); // create a Server object, and the object passed to the port number, which is to listen to the port 
24-      Connect (Server, the SIGNAL (UpdateServer (QString, int )), the this UpdateServer, SLOT ( (QString, int ))); // if the server sends updateServer signal to trigger the slot function tcpserver updateServer
 25      // so update dialog content 
26      CreateBtn-> setEnabled ( false ); // after creating the server can no longer click on the button 
27  }
 28  void tcpserver :: updateserver (MSG QString, int length) // update function of the groove 
29  {
 30      ContentListWidget->addItem(msg.left(length));
31 }

 


the second part:
. 1 Server :: Server (QObject * parent, int Port): QTcpServer, (parent)
 2  {
 . 3      the listen (QHostAddress the Any ::, Port); // with the specified port listening for any address 
. 4  }
 . 5  void Server :: incomingConnection ( socketDescriptor qintptr) // when there is a new connection time, TcpServer will trigger incomingConnection () function, each have a new connection will trigger once, that creates a new object 
6  {
 7      tcpClientSocket * tcpClientSocket = new new tcpClientSocket ( the this ); // Create a new client communication TcpClientSocket
 8      // emergence objects will create a new connection of a TcpClientSocket, if an incoming data connection, the object will be transmitted down through updateClient signal for reading 
9     Connect (tcpClientSocket, the SIGNAL (updateClients (QString, int )), the this , the SLOT (updateClients (QString, int ))); // connected TcpClientSocket updateClients signal of
 10      // when new data is incoming after, TcpClientSocket will issue updateClients signal, the triggering, the slot function updateClients Server, the signal incoming msg length with the slot function 
. 11      Connect (tcpClientSocket, the sIGNAL (disconnected ( int )), the this , the sLOT (slotDisconnected ( int ))); // connect the disconnected signal TcpClientSocket
 12      // when the object TcpClientSocket disconnected, the disconnected signals, triggering, Server, function slotDisconnected groove 
13 is      tcpClientSocket-> setSocketDescriptor (socketDescriptor);// The TcpClientSocket socket descriptor for the newly created parameter designated socketDescriptor 
14      tcpClientSocketList.append (tcpClientSocket); // add into this list tcpClientSocket tcpClientSocketList object 
15  }
 16  void Server :: updateClients (MSG QString, int length)
 . 17  {
 18 is      eMIT updateserver (MSG, length); // send a signal updateserver the notification server to update the corresponding dialog display state 
. 19      for ( int I = 0 ; I <tcpClientSocketList.count (); I ++) // implemented broadcasting, i.e. new data is sent to the broadcast server, it is sent to each object connection, synchronization update dialog box 
20      {
 21         * Item = tcpClientSocketList.at The QTcpSocket (I); //
 22 is          IF (! Item-> Write (msg.toLatin1 (), length) = length)
 23 is          {
 24              Continue ;
 25          }
 26 is      }
 27  }
 28  void Server :: slotDisconnected ( int descriptor) // TcpClientSocket object from the list of disconnected tcpClientSocketList will remove 
29  {
 30      for ( int I = 0 ; I <tcpClientSocketList.count (); I ++ ) {
 31 is          the QTcpSocket * Item = tcpClientSocketList.at (I) ;
32         if(item->socketDescriptor()==descriptor)
33         {
34             tcpClientSocketList.removeAt(i);
35             return;
36         }
37     }
38     return;
39 }

 

the third part:
 
. 1 TcpClientSocket :: TcpClientSocket (QObject * parent)
 2  {  
 . 3      Connect ( the this , the SIGNAL (the readyRead ()), the this , the SLOT (DateReceived The ())); // the readyRead () is a function of QIODevice signal, and inherited by QTcpSocket to, to the data signal when there is 
. 4      connect ( the this , the sIGNAL (disconnected ()), the this ;, the SLOT (slotDisconnected ())) // when disconnected () signal is a function of QIODevice by the QTcpSocket, disconnect signals 
. 5  }
 . 6  void TcpClientSocket :: DateReceived The ()
 . 7  {
 . 8      the while (the bytesAvailable ()> 0 ) //When there is data, the bytesAvailable () from the socket to the detected data, returns the number of bytes read incoming wait 
. 9      {
 10          int length = the bytesAvailable ();
 . 11          char buf [ 1024 ];
 12 is          Read ( buf, length); // read data to the socket buf, the read length of length 
13 is          QString MSG = buf;
 14          eMIT updateClients (MSG, length); // send a signal 
15      }
 16  }
 . 17  void :: slotDisconnected TcpClientSocket ()
 18 is  {
 . 19      EMIT disconnected ( the this -> socketDescriptor ()); //Issued disconnected signal 
20 }

 

Guess you like

Origin www.cnblogs.com/Emiya-Shirou/p/11261508.html