Article Title: how to send UDP datagram on an unconnected Socket. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
UDP is a simple datagram-oriented transport layer protocol. let's look at how to send a UDP datagram from the perspective of the UDP client and what the protocol stack has done to send a UDP datagram.
UDP datagram can be sent on an unconnected socket (using sendto system call, specifying the destination address), or on a connected socket (using the send system call, you do not need to specify the destination address). We will discuss it in two cases.
The following is an example of a user-state program that sends UDP data on an unconnected socket (note: the format and style of the program are rather bad, but they are only used for temporary testing .), This program only sends messages and does not process the messages. we will analyze the messages later:
# Include
# Include
# Include
# Include "my_inet.h" # include
# Include
# Include
Int main () {int I; struct sockaddr_in dest; dest. sin_family = MY_PF_INET; dest. sin_port = htons (16000); dest. sin_addr.s_addr = 0x013010AC; // the destination address is 172.16.48.1 (network byte order) // Create a socket for the UDP datagram service. Int fd = socket (MY_PF_INET, SOCK_DGRAM, MY_IPPROTO_UDP); if (fd <0) {perror ("socket:"); return-1;} int bwrite = sendto (fd, "abcdefg", 7, 0, (struct sockaddr *) & dest, sizeof (dest); if (bwrite =-1) {perror ("send :"); close (fd); return-1;} printf ("sendto: % d \ n", bwrite); close (fd); return 0 ;}
|
[1] [2] Next page