⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.html

📁 IEEE 1003.1-2003, Single Unix Specification v3
💻 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>connect</title></head><body bgcolor="white"><basefont size="3"> <a name="connect"></a> <a name="tag_03_85"></a><!-- connect --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright &copy; 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_85_01"></a>NAME</h4><blockquote>connect - connect a socket</blockquote><h4><a name="tag_03_85_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include &lt;<a href="../basedefs/sys/socket.h.html">sys/socket.h</a>&gt;<br><br> int connect(int</tt> <i>socket</i><tt>, const struct sockaddr *</tt><i>address</i><tt>,<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socklen_t</tt> <i>address_len</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_85_03"></a>DESCRIPTION</h4><blockquote><p>The <i>connect</i>() function shall attempt to make a connection on a socket. The function takes the following arguments:</p><dl compact><dt><i>socket</i></dt><dd>Specifies the file descriptor associated with the socket.</dd><dt><i>address</i></dt><dd>Points to a <b>sockaddr</b> structure containing the peer address. The length and format of the address depend on the addressfamily of the socket.</dd><dt><i>address_len</i></dt><dd>Specifies the length of the <b>sockaddr</b> structure pointed to by the <i>address</i> argument.</dd></dl><p>If the socket has not already been bound to a local address, <i>connect</i>() shall bind it to an address which, unless thesocket's address family is AF_UNIX, is an unused local address.</p><p>If the initiating socket is not connection-mode, then <i>connect</i>() shall set the socket's peer address, and no connection ismade. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent <a href="../functions/send.html"><i>send</i>()</a> functions, and limits the remote sender for subsequent <a href="../functions/recv.html"><i>recv</i>()</a> functions. If <i>address</i> is a null address for the protocol, the socket's peeraddress shall be reset.</p><p>If the initiating socket is connection-mode, then <i>connect</i>() shall attempt to establish a connection to the addressspecified by the <i>address</i> argument. If the connection cannot be established immediately and O_NONBLOCK is not set for thefile descriptor for the socket, <i>connect</i>() shall block for up to an unspecified timeout interval until the connection isestablished. If the timeout interval expires before the connection is established, <i>connect</i>() shall fail and the connectionattempt shall be aborted. If <i>connect</i>() is interrupted by a signal that is caught while blocked waiting to establish aconnection, <i>connect</i>() shall fail and set <i>errno</i> to [EINTR], but the connection request shall not be aborted, and theconnection shall be established asynchronously.</p><p>If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket,<i>connect</i>() shall fail and set <i>errno</i> to [EINPROGRESS], but the connection request shall not be aborted, and theconnection shall be established asynchronously. Subsequent calls to <i>connect</i>() for the same socket, before the connection isestablished, shall fail and set <i>errno</i> to [EALREADY].</p><p>When the connection has been established asynchronously, <a href="../functions/select.html"><i>select</i>()</a> and <a href="../functions/poll.html"><i>poll</i>()</a> shall indicate that the file descriptor for the socket is ready for writing.</p><p>The socket in use may require the process to have appropriate privileges to use the <i>connect</i>() function.</p></blockquote><h4><a name="tag_03_85_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, <i>connect</i>() shall return 0; otherwise, -1 shall be returned and <i>errno</i> set to indicatethe error.</p></blockquote><h4><a name="tag_03_85_05"></a>ERRORS</h4><blockquote><p>The <i>connect</i>() function shall fail if:</p><dl compact><dt>[EADDRNOTAVAIL]</dt><dd>The specified address is not available from the local machine.</dd><dt>[EAFNOSUPPORT]</dt><dd>The specified address is not a valid address for the address family of the specified socket.</dd><dt>[EALREADY]</dt><dd>A connection request is already in progress for the specified socket.</dd><dt>[EBADF]</dt><dd>The <i>socket</i> argument is not a valid file descriptor.</dd><dt>[ECONNREFUSED]</dt><dd>The target address was not listening for connections or refused the connection request.</dd><dt>[EINPROGRESS]</dt><dd>O_NONBLOCK is set for the file descriptor for the socket and the connection cannot be immediately established; the connectionshall be established asynchronously.</dd><dt>[EINTR]</dt><dd>The attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection shall beestablished asynchronously.</dd><dt>[EISCONN]</dt><dd>The specified socket is connection-mode and is already connected.</dd><dt>[ENETUNREACH]</dt><dd>No route to the network is present.</dd><dt>[ENOTSOCK]</dt><dd>The <i>socket</i> argument does not refer to a socket.</dd><dt>[EPROTOTYPE]</dt><dd>The specified address has a different type than the socket bound to the specified peer address.</dd><dt>[ETIMEDOUT]</dt><dd>The attempt to connect timed out before a connection was made.</dd></dl><p>If the address family of the socket is AF_UNIX, then <i>connect</i>() shall 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>[ELOOP]</dt><dd>A loop exists in symbolic links encountered during resolution of the pathname in <i>address</i>.</dd><dt>[ENAMETOOLONG]</dt><dd>A component of a pathname exceeded {NAME_MAX} characters, or an entire pathname exceeded {PATH_MAX} characters.</dd><dt>[ENOENT]</dt><dd>A component of the pathname does not name an existing file or the pathname is an empty string.</dd><dt>[ENOTDIR]</dt><dd>A component of the path prefix of the pathname in <i>address</i> is not a directory.</dd></dl><p>The <i>connect</i>() function may fail if:</p><dl compact><dt>[EACCES]</dt><dd>Search permission is denied for a component of the path prefix; or write access to the named socket is denied.</dd><dt>[EADDRINUSE]</dt><dd>Attempt to establish a connection that uses addresses that are already in use.</dd><dt>[ECONNRESET]</dt><dd>Remote host reset the connection request.</dd><dt>[EHOSTUNREACH]</dt><dd>The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).</dd><dt>[EINVAL]</dt><dd>The <i>address_len</i> argument is not a valid length for the address family; or invalid address family in the <b>sockaddr</b>structure.</dd><dt>[ELOOP]</dt><dd>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in <i>address</i>.</dd><dt>[ENAMETOOLONG]</dt><dd>Pathname resolution of a symbolic link produced an intermediate result whose length exceeds {PATH_MAX}.</dd><dt>[ENETDOWN]</dt><dd>The local network interface used to reach the destination is down.</dd><dt>[ENOBUFS]</dt><dd>No buffer space is available.</dd><dt>[EOPNOTSUPP]</dt><dd>The socket is listening and cannot be connected.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_85_06"></a>EXAMPLES</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_85_07"></a>APPLICATION USAGE</h4><blockquote><p>If <i>connect</i>() fails, the state of the socket is unspecified. Conforming applications should close the file descriptor andcreate a new socket before attempting to reconnect.</p></blockquote><h4><a name="tag_03_85_08"></a>RATIONALE</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_85_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_85_10"></a>SEE ALSO</h4><blockquote><p><a href="accept.html"><i>accept</i>()</a> , <a href="bind.html"><i>bind</i>()</a> , <a href="close.html"><i>close</i>()</a> , <ahref="getsockname.html"><i>getsockname</i>()</a> , <a href="poll.html"><i>poll</i>()</a> , <a href="select.html"><i>select</i>()</a> , <a href="send.html"><i>send</i>()</a> , <a href="shutdown.html"><i>shutdown</i>()</a> , <ahref="socket.html"><i>socket</i>()</a> , the Base Definitions volume of IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/sys/socket.h.html"><i>&lt;sys/socket.h&gt;</i></a></p></blockquote><h4><a name="tag_03_85_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 6. Derived from the XNS, Issue 5.2 specification.</p><p>The wording of the mandatory [ELOOP] error condition is updated, and a second optional [ELOOP] error condition is added.</p></blockquote><div class="box"><em>End of informative text.</em></div><hr><hr size="2" noshade><center><font size="2"><!--footer start-->UNIX &reg; is a registered Trademark of The Open Group.<br>POSIX &reg; 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 + -