Socket programming in QT

Source: Internet
Author: User
For network programming in Linux, we can use the unified socket interface provided by Linux. However, this method involves too many structs, such as IP address and port conversion. unskilled people often make such errors. The socket provided in QT completely uses the class encapsulation mechanism, so that users do not need to access the underlying structure operations. In addition, it uses the signal-Slot Mechanism of QT to make the program easier to understand.

Qt provides four classes related to the set of words:

Qserversocket: TCP-based Server
Qsocket: buffered TCP Connection
Qsocketdevice: platform-independent low-level Socket API
Qsocketnotifier: Support for socket callbacks

The following describes how to use QT for network programming. We use a simple C/S network program to illustrate how to use sockets in QT. At the same time, we use TCP and UDP protocols to implement this program (the program client and the server each send a character port string "ABC" to the other party ")

1. UDP implementation
UDP is a non-connection protocol without the concept of a client or a server.
1) create a socket-Related Object
Qsocketdevice * mureceivesocket; // socket object
Qsocketnotifier * msocketnotifier; // The socket listener object.
2) initialize socket-related objects
Mureceivesocket = new qsocketdevice (qsocketdevice: datagram );
// UDP Initialization
Qhostaddress myaddress;
Qstring fakeaddress;
Fakeaddress = get_eth1_ip (); // obtain the interface IP Address
Myaddress. setaddress (fakeaddress );
Mureceivesocket-> BIND (myaddress, Port );
// Bind to the specified network interface address (IP) and specify the logical Port

Msocketnotifier = new qsocketnotifier (mureceivesocket-> socket (), qsocketnotifier: read, 0, "msocketnotifier ");
// Listen to mureceivesocket
3) Implement the response slot for definition purposes
Virtual void onmreceive ();
Void client: onmreceive ()
{
Int bytecount, readcount;
Char * incommingchar;

Fprintf (stderr, "load a piece of message! \ N ");

Bytecount = mureceivesocket-> bytesavailable ();
Incommingchar = (char *) malloc (bytecount + 1 );
Readcount = mureceivesocket-> readblock (incommingchar, bytecount );
Incommingchar [bytecount] = '\ 0 ';

Fprintf (stderr, "% s", incommingchar); // print the received string
}
4) associate the signal of the socket and the receiving slot
Connect (msocketnotifier, signal (activated (INT), this, slot (onmreceive ()));
// Call onmreceive when msocketnotifier detects mureceivesocket is active
5) Send a string
Char information [20];
Strcpy (information, "ABC");
Mureceivesocket-> writeblock (information, length, myaddress, 2201 );
2. TCP implementation
The implementation of TCP is similar to that of UDP. It is a face-to-face connection protocol. This section only describes the difference between UDP and UDP.
Server:
1) socket object definition
A socket is defined more than UDP, one for listening port and one for communication.
Create a qssocket class to inherit qserversocket
Qssocket * serversocket; // TCP-based Server
Qsocketdevice * clientsocket;
Qsocketnotifier * clientnotifier;
Qsocketnotifier * servernotifier;
2) socket Initialization
Qhostaddress myaddress;
Qstring fakeaddress;

Fakeaddress = "127.0.0.1 ";
Myaddress. setaddress (fakeaddress );
Udint Port = 1234;
Serversocket = new qssocket (myaddress, port, this, 0); // specify the listening address and port
// Qserversocket class can also be used here
Clientsocket = new qsocketdevice (qsocketdevice: Stream );

Clienttnotifier = new qsocketnotifier (clientsocket-> socket (), qsocketnotifier: read, 0, "clientsocket ");
3) response connection
You only need to add the following code to the constructor of qssocket:
Serversocket-> newconncetion (clientsocket-> socket ());
After receiving the connection from the client, clientsocket automatically responds and receives data.

4) the slot of the received information is consistent with that of UDP, which is not described here.
Client implementation:
The client implementation is similar to the UDP implementation. The difference is that the client socket does not need the BIND port, because TCP will keep the connection after connecting to the server end until the communication ends.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.