{"id":4732,"date":"2022-12-20T18:36:50","date_gmt":"2022-12-20T21:36:50","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/connect-man3p\/"},"modified":"2022-12-20T18:36:50","modified_gmt":"2022-12-20T21:36:50","slug":"connect-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/connect-man3p\/","title":{"rendered":"CONNECT (man3p)"},"content":{"rendered":"<h1 align=\"center\">CONNECT<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">connect \u2014 connect a socket<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <sys\/socket.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int connect(int <i>socket<\/i>, const struct sockaddr *<i>address<\/i>, <br \/> socklen_t <i>address_len<\/i>);<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>connect<\/i>() function shall attempt to make a connection on a connection-mode socket or to set or reset the peer address of a connectionless-mode socket. The function takes the following arguments:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>socket<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Specifies the file descriptor associated with the socket.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>address<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Points to a <b>sockaddr<\/b> structure containing the peer address. The length and format of the address depend on the address family of the socket.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"17%\">\n<p><i>address_len<\/i><\/p>\n<\/td>\n<td width=\"1%\"><\/td>\n<td width=\"71%\">\n<p>Specifies the length of the <b>sockaddr<\/b> structure pointed to by the <i>address<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\">If the socket has not already been bound to a local address, <i>connect<\/i>() shall bind it to an address which, unless the socket\u2019s address family is AF_UNIX, is an unused local address.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the initiating socket is not connection-mode, then <i>connect<\/i>() shall set the socket\u2019s peer address, and no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent <i>send<\/i>() functions, and limits the remote sender for subsequent <i>recv<\/i>() functions. If the <i>sa_family<\/i> member of <i>address<\/i> is AF_UNSPEC, the socket\u2019s peer address shall be reset. Note that despite no connection being made, the term \u2018\u2018connected\u2019\u2019 is used to describe a connectionless-mode socket for which a peer address has been set.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the initiating socket is connection-mode, then <i>connect<\/i>() shall attempt to establish a connection to the address specified by the <i>address<\/i> argument. If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, <i>connect<\/i>() shall block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, <i>connect<\/i>() shall fail and the connection attempt shall be aborted. If <i>connect<\/i>() is interrupted by a signal that is caught while blocked waiting to establish a connection, <i>connect<\/i>() shall fail and set <i>errno<\/i> to <b>[EINTR]<\/b>, but the connection request shall not be aborted, and the connection shall be established asynchronously.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 <b>[EINPROGRESS]<\/b>, but the connection request shall not be aborted, and the connection shall be established asynchronously. Subsequent calls to <i>connect<\/i>() for the same socket, before the connection is established, shall fail and set <i>errno<\/i> to <b>[EALREADY]<\/b>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">When the connection has been established asynchronously, <i>pselect<\/i>(), <i>select<\/i>(), and <i>poll<\/i>() shall indicate that the file descriptor for the socket is ready for writing.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The socket in use may require the process to have appropriate privileges to use the <i>connect<\/i>() function.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, <i>connect<\/i>() shall return 0; otherwise, \u22121 shall be returned and <i>errno<\/i> set to indicate the error.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>connect<\/i>() function shall fail if: <b><br \/> EADDRNOTAVAIL<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address is not available from the local machine.<\/p>\n<p style=\"margin-left:11%;\"><b>EAFNOSUPPORT<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address is not a valid address for the address family of the specified socket.<\/p>\n<p style=\"margin-left:11%;\"><b>EALREADY<\/b><\/p>\n<p style=\"margin-left:22%;\">A connection request is already in progress for the specified socket.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EBADF<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>The <i>socket<\/i> argument is not a valid file descriptor.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ECONNREFUSED<\/b><\/p>\n<p style=\"margin-left:22%;\">The target address was not listening for connections or refused the connection request.<\/p>\n<p style=\"margin-left:11%;\"><b>EINPROGRESS<\/b><\/p>\n<p style=\"margin-left:22%;\">O_NONBLOCK is set for the file descriptor for the socket and the connection cannot be immediately established; the connection shall be established asynchronously.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EINTR<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>The attempt to establish a connection was interrupted by delivery of a signal that was caught; the connection shall be established asynchronously.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EISCONN<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified socket is connection-mode and is already connected.<\/p>\n<p style=\"margin-left:11%;\"><b>ENETUNREACH<\/b><\/p>\n<p style=\"margin-left:22%;\">No route to the network is present.<\/p>\n<p style=\"margin-left:11%;\"><b>ENOTSOCK<\/b><\/p>\n<p style=\"margin-left:22%;\">The <i>socket<\/i> argument does not refer to a socket.<\/p>\n<p style=\"margin-left:11%;\"><b>EPROTOTYPE<\/b><\/p>\n<p style=\"margin-left:22%;\">The specified address has a different type than the socket bound to the specified peer address.<\/p>\n<p style=\"margin-left:11%;\"><b>ETIMEDOUT<\/b><\/p>\n<p style=\"margin-left:22%;\">The attempt to connect timed out before a connection was made.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the address family of the socket is AF_UNIX, then <i>connect<\/i>() shall fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>EIO<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>An I\/O error occurred while reading from or writing to the file system.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"7%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"4%\"><\/td>\n<td width=\"78%\">\n<p>A loop exists in symbolic links encountered during resolution of the pathname in <i>address<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a component of a pathname is longer than {NAME_MAX}.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A component of the pathname does not name an existing file or the pathname is an empty string.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">A component of the path prefix of the pathname in <i>address<\/i> names an existing file that is neither a directory nor a symbolic link to a directory, or the pathname in <i>address<\/i> contains at least one non-<slash> character and ends with one or more trailing <slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>connect<\/i>() function may fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Search permission is denied for a component of the path prefix; or write access to the named socket is denied.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>EADDRINUSE<\/b><\/p>\n<p style=\"margin-left:22%;\">Attempt to establish a connection that uses addresses that are already in use.<\/p>\n<p style=\"margin-left:11%;\"><b>ECONNRESET<\/b><\/p>\n<p style=\"margin-left:22%;\">Remote host reset the connection request.<\/p>\n<p style=\"margin-left:11%;\"><b>EHOSTUNREACH<\/b><\/p>\n<p style=\"margin-left:22%;\">The destination host cannot be reached (probably because the host is down or a remote router cannot reach it).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EINVAL<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>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.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the pathname in <i>address<\/i>.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a pathname exceeds {PATH_MAX}, or pathname resolution of a symbolic link produced an intermediate result with a length that exceeds {PATH_MAX}.<\/p>\n<p style=\"margin-left:11%;\"><b>ENETDOWN<\/b><\/p>\n<p style=\"margin-left:22%;\">The local network interface used to reach the destination is down.<\/p>\n<p style=\"margin-left:11%;\"><b>ENOBUFS<\/b><\/p>\n<p style=\"margin-left:22%;\">No buffer space is available.<\/p>\n<p style=\"margin-left:11%;\"><b>EOPNOTSUPP<\/b><\/p>\n<p style=\"margin-left:22%;\">The socket is listening and cannot be connected.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>connect<\/i>() fails, the state of the socket is unspecified. Conforming applications should close the file descriptor and create a new socket before attempting to reconnect.<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>accept<\/i>(), <i>bind<\/i>(), <i>close<\/i>(), <i>getsockname<\/i>(), <i>poll<\/i>(), <i>pselect<\/i>(), <i>send<\/i>(), <i>shutdown<\/i>(), <i>socket<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><sys_socket.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http:\/\/www.opengroup.org\/unix\/online.html .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https:\/\/www.kernel.org\/doc\/man-pages\/reporting_bugs.html .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  connect \u2014 connect a socket <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1653,1594],"class_list":["post-4732","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-connect","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4732"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4732\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}