Socket programming in Qt

xiaoxiao2021-03-06  40

Operating system: ARM-LinuxQT version: Qt-2.3.2-for-linuxgui: Qtopia is programmed under Linux, and we can use the unified socket interface provided by Linux. However, this approach involves too many structures, such as IP addresses, port conversion, etc., unskilled people often make such errors like this. The Socket provided in Qt fully uses the classes of the package, and users do not need to contact the underlying structural operations. And it uses the QT itself's Signal-Slot mechanism to make the written program easier to understand. QT CCP provides four sets of classes and word-related, namely: QServerSocket: TCP-based serverQSocket: Buffered TCP connectionQSocketDevice: Platform-independent low-level socket APIQSocketNotifier: Support for socket callbacks are described below using QT network programming, we use A simple C / S mode network program illustrates how to use the socket in QT. At the same time, we use TCP and UDP to implement this program (the program client and the server send a character port "ABC") 1, the UDP implementation UDP is an unconnected protocol, no client and server concept. 1) Establish a socketDered object qSocketDevice * MuReceiveSocket; // socket object QSocketNotifier * msocketNotifier; // Socket monitor object 2) Initializing socket related objects MureceviceSocket = new qsocketDevice (QSocketDevice :: datagram); // UDP initialization QHostaddress myaddress; QString FakeAddress;

FakeAddress = get_eth1_ip (); // get an interface to IP MyAddress.setAddress (FakeAddress); MUReceiveSocket-> bind (MyAddress, Port); // bind to a specific network interface address (IP), specifies the logical port MSocketNotifier = new QSocketNotifier (MUReceiveSocket -> Socket (), qsocketnotifier :: read, 0, "msocketnotifier"); // Listening MURECESOCKET socket 3) Define the implementation response Slot Virtual void onmreceive (); void client :: onmreceive () {int Bytecount, ReadCount CHAR * incommingcha;

FPRINTF (stderr, "loading 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 received string} 4) Signal and receive Slot Connect (Msocate), Signal (onMRecEther ())); // MSocketNotifier detecting call OnMReceive 5) to transmit the string when MUReceiveSocket active char information [20]; strcpy (information, "abc"); to achieve 2, TCP and TCP achieved; MUReceiveSocket-> writeBlock (information, length, MyAddress, 2201) The implementation of UDP is similar, which is an agreement with an iconic connection. Here is only different from UDP. Server: 1) The definition of the socket object is more than the UDP multi-socket, one to listen to the port, one is used to communicate. Establish a QSSocket class inherits QServerSocket QSSocket * ServerSocket; // TCP-based server QSocketDevice * ClientSocket; QSocketNotifier * ClientNotifier; QSocketNotifier * ServerNotifier; 2) socket initialization QHostAddress MyAddress; QString FakeAddress;

转载请注明原文地址:https://www.9cbs.com/read-65914.html

New Post(0)