How to send UDP datagram on an unconnected Socket

Source: Internet
Author: User
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

Related Article

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.