Talk C Chestnut Bar (150th: C language Instance--socket communication Interface II)

Source: Internet
Author: User
Tags sendmsg

Ladies and gentlemen, crossing, the last time we were talking about the socket communication example, let's go on to this example. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we described the system calls used during socket communication in the previous section, which are generic and can be used in any type of socket communication. Today, we will introduce some system calls that are specifically designed to send and receive data through sockets. The functions of these system calls are very similar, but differ in the details of the operation. Next we introduce them separately.

System calls to send data
ssize_t Send (int  sockfd, const  void  *buf, size_t Len, int  flags) ssize_t sendto (int  sockfd, const  void  *buf, size_t Len, int  flags, const  struct  sockaddr *dest_addr, socklen_t addrlen) ssize_t sendmsg (int  sockfd, const  struct  msghdr *msg, int  flags)  
    • These three function functions are used to send data, and their functions are the same as the function of the write function, but send the different ways;
    • The first parameter in these three functions is a socket descriptor;
    • The parameter buf in these three functions are the buffers used to hold the data, and the parameter Len indicates the number of bytes of data in the buffer;
    • The flags parameter in these three functions represents the way the data is sent, the commonly used value is msg_dontwait, which means that the data is sent in a non-blocking manner, and we can also use 0 to assign it, at which point the data is sent in exactly the same way as the write function writes the data The flags have other values that are network-related and involve a lot of network knowledge, so we don't go into detail.
    • The parameter dest_addr in the SendTo function is the address of the data receiving place, and the parameter addr_len is the length of the address;
    • The MSG in the SENDMSG function is a pointer to a struct type that contains parameters from several other functions, such as the address of the communication, the flags, and the buffer address. In addition, it also contains a number of special members, we do not do a detailed introduction here, in the future when we meet to give you a detailed introduction.
System calls to receive data
ssize_t recv  (int  sockfd, void *buf , size_t Len, int  flags) ssize_t recvfrom (int  sockfd, void Span class= "hljs-variable" >*buf , size_t len, int  flags, ST Ruct sockaddr *src_addr , socklen_t *addrlen ) ssize_t recvmsg (int  sockfd, struct MSGHDR *msg  , int  flags)  
    • These three functions are used to receive data, and their functions are the same as the function of the read function, but receive different ways;
    • The first parameter in these three functions is a socket descriptor;
    • The parameter buf in these three functions are the buffers used to hold the data, and the parameter Len indicates the number of bytes of data in the buffer;
    • The flags parameter in these three functions represents the way the data is sent, the commonly used value is msg_dontwait, which means that the data is received in a non-blocking manner, and we can use 0 to assign a value to it, when the data is received in exactly the same way as the read function reads the data The flags have other values that are network-related and involve a lot of network knowledge, so we don't go into detail.
    • The parameter src_addr in the Recvfrom function is the address where the data is sent, the parameter addr_len is the length of the address, the function can get the server address to send the data, if you do not want to know the address, you can use a null pointer;
    • The MSG in the RECVMSG function is a pointer to a struct type that contains parameters from several other functions, such as the address of the communication, the flags, and the buffer address. In addition, it also contains a number of special members, we do not do a detailed introduction here, in the future when we meet to give you a detailed introduction.

Say so many functions, we are not feel a bit chaotic, and face so many functions, do not know how to choose. Next, we give some reference advice.

    • Typically, the SEND/RECV function is used more often when using a stream socket, and two other functions can be used, of course.
    • When using a datagram socket, the Sendto/recvfrom function uses more, and the SEND/RECV function is not available because it does not specify the address of the communication.
    • The SENDMSG/RECVMSG function is primarily used to pass data from multiple buffers, which are not limited by the type of socket and are widely used.

You crossing, for example of socket communication interface Let's just say here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (150th: C language Instance--socket communication Interface II)

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.