📄 recvmsg.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta name="generator" content="HTML Tidy, see www.w3.org"><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 --><!-- Copyright (c) 2001-2003 The Open Group, All Rights Reserved --><title>recvmsg</title></head><body bgcolor="white"><basefont size="3"> <a name="recvmsg"></a> <a name="tag_03_602"></a><!-- recvmsg --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_602_01"></a>NAME</h4><blockquote>recvmsg - receive a message from a socket</blockquote><h4><a name="tag_03_602_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/sys/socket.h.html">sys/socket.h</a>><br><br> ssize_t recvmsg(int</tt> <i>socket</i><tt>, struct msghdr *</tt><i>message</i><tt>, int</tt> <i>flags</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_602_03"></a>DESCRIPTION</h4><blockquote><p>The <i>recvmsg</i>() function shall receive a message from a connection-mode or connectionless-mode socket. It is normally usedwith connectionless-mode sockets because it permits the application to retrieve the source address of received data.</p><p>The <i>recvmsg</i>() function takes the following arguments:</p><dl compact><dt><i>socket</i></dt><dd>Specifies the socket file descriptor.</dd><dt><i>message</i></dt><dd>Points to a <b>msghdr</b> structure, containing both the buffer to store the source address and the buffers for the incomingmessage. The length and format of the address depend on the address family of the socket. The <i>msg_flags</i> member is ignored oninput, but may contain meaningful values on output.</dd><dt><i>flags</i></dt><dd>Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the followingvalues: <dl compact><dt>MSG_OOB</dt><dd>Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.</dd><dt>MSG_PEEK</dt><dd>Peeks at the incoming message.</dd><dt>MSG_WAITALL</dt><dd>On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. The function mayreturn the smaller amount of data if the socket is a message-based socket, if a signal is caught, if the connection is terminated,if MSG_PEEK was specified, or if an error is pending for the socket.</dd></dl></dd></dl><p>The <i>recvmsg</i>() function shall receive messages from unconnected or connected sockets and shall return the length of themessage.</p><p>The <i>recvmsg</i>() function shall return the total length of the message. For message-based sockets, such as SOCK_DGRAM andSOCK_SEQPACKET, the entire message shall be read in a single operation. If a message is too long to fit in the supplied buffers,and MSG_PEEK is not set in the <i>flags</i> argument, the excess bytes shall be discarded, and MSG_TRUNC shall be set in the<i>msg_flags</i> member of the <b>msghdr</b> structure. For stream-based sockets, such as SOCK_STREAM, message boundaries shall beignored. In this case, data shall be returned to the user as soon as it becomes available, and no data shall be discarded.</p><p>If the MSG_WAITALL flag is not set, data shall be returned only up to the end of the first message.</p><p>If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, <i>recvmsg</i>() shallblock until a message arrives. If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor,the <i>recvmsg</i>() function shall fail and set <i>errno</i> to [EAGAIN] or [EWOULDBLOCK].</p><p>In the <b>msghdr</b> structure, the <i>msg_name</i> and <i>msg_namelen</i> members specify the source address if the socket isunconnected. If the socket is connected, the <i>msg_name</i> and <i>msg_namelen</i> members shall be ignored. The <i>msg_name</i>member may be a null pointer if no names are desired or required. The <i>msg_iov</i> and <i>msg_iovlen</i> fields are used tospecify where the received data shall be stored. <i>msg_iov</i> points to an array of <b>iovec</b> structures; <i>msg_iovlen</i>shall be set to the dimension of this array. In each <b>iovec</b> structure, the <i>iov_base</i> field specifies a storage area andthe <i>iov_len</i> field gives its size in bytes. Each storage area indicated by <i>msg_iov</i> is filled with received data inturn until all of the received data is stored or all of the areas have been filled.</p><p>Upon successful completion, the <i>msg_flags</i> member of the message header shall be the bitwise-inclusive OR of all of thefollowing flags that indicate conditions detected for the received message:</p><dl compact><dt>MSG_EOR</dt><dd>End-of-record was received (if supported by the protocol).</dd><dt>MSG_OOB</dt><dd>Out-of-band data was received.</dd><dt>MSG_TRUNC</dt><dd>Normal data was truncated.</dd><dt>MSG_CTRUNC</dt><dd>Control data was truncated.</dd></dl></blockquote><h4><a name="tag_03_602_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>recvmsg</i>() shall return the length of the message in bytes. If no messages are available to bereceived and the peer has performed an orderly shutdown, <i>recvmsg</i>() shall return 0. Otherwise, -1 shall be returned and<i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_602_05"></a>ERRORS</h4><blockquote><p>The <i>recvmsg</i>() function shall fail if:</p><dl compact><dt>[EAGAIN] or [EWOULDBLOCK]</dt><dd><br>The socket's file descriptor is marked O_NONBLOCK and no data is waiting to be received; or MSG_OOB is set and no out-of-band datais available and either the socket's file descriptor is marked O_NONBLOCK or the socket does not support blocking to awaitout-of-band data.</dd><dt>[EBADF]</dt><dd>The <i>socket</i> argument is not a valid open file descriptor.</dd><dt>[ECONNRESET]</dt><dd>A connection was forcibly closed by a peer.</dd><dt>[EINTR]</dt><dd>This function was interrupted by a signal before any data was available.</dd><dt>[EINVAL]</dt><dd>The sum of the <i>iov_len</i> values overflows a <b>ssize_t</b>, or the MSG_OOB flag is set and no out-of-band data isavailable.</dd><dt>[EMSGSIZE]</dt><dd>The <i>msg_iovlen</i> member of the <b>msghdr</b> structure pointed to by <i>message</i> is less than or equal to 0, or isgreater than {IOV_MAX}.</dd><dt>[ENOTCONN]</dt><dd>A receive is attempted on a connection-mode socket that is not connected.</dd><dt>[ENOTSOCK]</dt><dd>The <i>socket</i> argument does not refer to a socket.</dd><dt>[EOPNOTSUPP]</dt><dd>The specified flags are not supported for this socket type.</dd><dt>[ETIMEDOUT]</dt><dd>The connection timed out during connection establishment, or due to a transmission timeout on active connection.</dd></dl><p>The <i>recvmsg</i>() function may fail if:</p><dl compact><dt>[EIO]</dt><dd>An I/O error occurred while reading from or writing to the file system.</dd><dt>[ENOBUFS]</dt><dd>Insufficient resources were available in the system to perform the operation.</dd><dt>[ENOMEM]</dt><dd>Insufficient memory was available to fulfill the request.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_602_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_602_07"></a>APPLICATION USAGE</h4><blockquote><p>The <a href="../functions/select.html"><i>select</i>()</a> and <a href="../functions/poll.html"><i>poll</i>()</a> functions canbe used to determine when data is available to be received.</p></blockquote><h4><a name="tag_03_602_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_602_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_602_10"></a>SEE ALSO</h4><blockquote><p><a href="poll.html"><i>poll</i>()</a> , <a href="recv.html"><i>recv</i>()</a> , <a href="recvfrom.html"><i>recvfrom</i>()</a> ,<a href="select.html"><i>select</i>()</a> , <a href="send.html"><i>send</i>()</a> , <a href="sendmsg.html"><i>sendmsg</i>()</a> ,<a href="sendto.html"><i>sendto</i>()</a> , <a href="shutdown.html"><i>shutdown</i>()</a> , <a href="socket.html"><i>socket</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/sys/socket.h.html"><i><sys/socket.h></i></a></p></blockquote><h4><a name="tag_03_602_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 6. Derived from the XNS, Issue 5.2 specification.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX ® is a registered Trademark of The Open Group.<br>POSIX ® is a registered Trademark of The IEEE.<br>[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href="../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>]</font></center><!--footer end--><hr size="2" noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -