Linux System Call-Detailed description of the Recv/recvfrom/recvmsg Function

Source: Internet
Author: User
Tags socket error
[Recv/recvfrom/recvmsg System Call] function description:
Receives a message from a socket. For recvfrom and recvmsg, it can be applied to connection-oriented and connectionless sockets at the same time. Generally, Recv is only used in connection-oriented sockets. It is almost equivalent to recvfrom. You only need to set the fifth parameter of recvfrom to null. If the message is too large to be fully stored in the provided buffer, additional bytes are discarded based on different sockets. If no message can be read on the socket, except that the socket has been set to non-blocking mode, the receiving and calling will wait for the message to arrive. Usage:
# Include <sys/types. h>
# Include <sys/socket. h> ssize_t Recv (INT sock, void * Buf, size_t Len, int flags); ssize_t recvfrom (INT sock, void * Buf, size_t Len, int flags,
Struct sockaddr * From, socklen_t * fromlen); ssize_t recvmsg (INT sock, struct msghdr * MSG, int flags); parameter:
Sock: the socket from which the index will receive data.
Buf: the buffer that stores the received message.
Len: the buffer capacity specified by BUF.
Flags: a combination of the following or multiple logos. You can use the or operation to connect them together. msg_dontwait: The operation will not be blocked.
Msg_errqueue:
Indicates that the error value should be received from the error queue of the socket. According to different protocols, the error value is transmitted in a supplementary message,
The user should provide a buffer that is large enough. The original packet that causes the error is transmitted using msg_iovec as the normal data. The original destination address of the incorrect datagram is provided as msg_name.
The error is used in the sock_extended_err structure and is defined as follows # define so_ee_origin_none 0
# Define so_ee_origin_local 1
# Define so_ee_origin_icmp 2
# Define so_ee_origin_icmp6 3 struct sock_extended_err
{
U_int32_t ee_errno;/* error number */
U_int8_t ee_origin;/* Where the error originated */
U_int8_t ee_type;/* type */
U_int8_t ee_code;/* Code */
U_int8_t ee_pad;
U_int32_t ee_info;/* Additional information */
U_int32_t ee_data;/* Other data */
/* More data may follow */
}; Msg_peek: indicates that the original data is retained in the receiving queue and will not be deleted after the data is received. Subsequent read operations can also receive the same data.
Msg_trunc: returns the actual length of the packet, which is only valid for packet sockets even if it is longer than the provided buffer.
Msg_waitall: blocking operation is required until the request is fully satisfied. However, if a signal is captured, an error occurs, or the connection is disconnected, or the data type to be received is different next time, data smaller than the Request volume will still be returned.
Msg_eor: indicates the end of the record. The returned data completes a record.
Msg_trunc: indicates that the data at the end of the datagram has been discarded because it requires more space than the provided buffer.
Msg_ctrunc: indicates that some control data has been discarded due to insufficient buffer space.
MSG_OOB: indicates that the out-of-band data is received (that is, the data to be processed preferentially ).
Msg_errqueue: indicates that no data is received except for errors from the socket error queue. From: point to the region where the peer address is stored. If it is null, the peer address is not stored.
Fromlen: as the entry parameter, it points to the memory unit that stores the largest from capacity. As an exit parameter, point to the memory unit that stores the actual length from.
MSG: indicates the memory buffer that stores the message header. The structure is as follows: struct msghdr {
Void * msg_name;/* optional address */
Socklen_t msg_namelen;/* size of address */
Struct iovec * msg_iov;/* scatter/gather array */
Size_t msg_iovlen;/* # elements in msg_iov */
Void * msg_control;/* ancillary data, see below */
Socklen_t msg_controllen;/* ancillary data buffer Len */
Int msg_flags;/* flags on received message */
}; The data structures that may be used include struct cmsghdr {
Socklen_t cmsg_len;/* Data byte count, including HDR */
Int cmsg_level;/* originating Protocol */
Int cmsg_type;/* protocol-specific type */
/* Followed
U_char cmsg_data []; */
}; Return instructions:
When execution is successful, the number of received bytes is returned. If the other end is closed, 0 is returned. -1 is returned for failure, and errno is set to one of the following values
Eagain: the socket is marked as non-blocking, and the receiving operation is blocked or the receiving times out.
Ebadf: sock is not a valid descriptive word.
Econnrefuse: remote host blocks Network Connections
Efault: memory space access error
Eintr: The operation is interrupted.
Einval: the parameter is invalid.
Enomem: insufficient memory
Enotconn: the socket associated with the connection-oriented has not been connected.
Enotsock: the sock index is not a socket

 

From: [url] http://club.cn.yahoo.com/bbs/threadview/1200062866_65__pn.html [/url]

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.