Xunwei iTOP-i.MX6ULL development board-network communication-socket UDP
[Copy link]
The data used in this chapter has been put into the development board network disk data, the path is: "11_Linux system development advanced\78_Chapter_Network communication-Socket UDP usage data".
iTOP-IMX6ULL implements socket programming based on UDP.
78.1 Experimental operation
UDP is the user datagram protocol, which is a connectionless transport layer protocol that provides simple and unreliable information transmission services for things. Therefore, in some environments where the network quality is not satisfactory, the loss of UDP protocol data packets will be more serious, which will cause data loss. The characteristic of UDP is that it is not a connection-type protocol, so the resource consumption is small. The advantage of fast processing speed, so usually audio, video and ordinary data are more used in UDP transmission, because even if they occasionally lose one or two data packets, it will not have much impact on the receiving result. The communication block
diagram of UDP is shown in Figure 78.1.1
From the above figure, you can see that the program based on UDP communication is also composed of server and client. Unlike TCP servers, UDP servers do not need listen and accept functions, and clients do not need connect functions. Let's take an example to understand UDP applications. First, let's look at the server program.
The code above is similar to the TCP server program in the previous chapter. First, some variables are defined, and then the
socket function is called to create a socket. The second parameter of the socket function here is SOCK_DGRAM, which is the user datagram, that is, the UDP connection created. Then the bzero function is called to clear the value of the variable servaddr, and then the value of the servaddr structure is set. Then the bind function is called, and then the recvfrom function is called to directly receive data without calling the listen and accept functions.
Let's take a look at the client program client.c:
The client program first defines some variables to be used, and then calls the socket function to create a socket. The second parameter of the socket function is SOCK_DGRAM, which is the user datagram, that is, the UDP connection. Then call the bzero function to clear the variables of the servaddr structure, and then set the value of the servaddr structure variable, and then call the sendto function. Here the client program does not call the bind function and the connect function. Now use the command.
gcc -o server server.c
The execution result is shown in Figure 78.1.2:
Use the command arm-none-linux-gnueabi-gcc -o client client.c to compile client.c. The execution result is shown in Figure 78.1.3.
Copy the generated client to the NFS shared directory, as shown in Figure 78.1.4
Then run the server program on the virtual machine Ubuntu, as shown in Figure 78.1.5.
Run the client program on the iTOP-IMX6ULL development board, as shown in Figure 78.1.6.
Now look at the output of the server program of the virtual machine Ubuntu, as shown in Figure 78.1.7:
You can see that the server program outputs "Hello, word", and the printed information above is the data sent by the client. At this point, socket communication based on UDP is realized.
|