connect()

PROTOTYPE

#include <sockets.h>

int connect(int socket, const void *addr, socklen_t addrlen);

DESCRIPTION

connect() assigns a remote address to a socket and, for TCP sockets, sends a connection request. socket is a handle returned by socket(). addr points to a structure (sockaddr_in or sockaddr_in6) containing the remote address. This determines where packets are sent to and accepted from. addrlen must greater than or equal to the socket address size.

For TCP sockets, connect() initiates the three-step SYN-ACK connection handshake with the specified host. If the socket is blocking, connect() waits until the exchange has completed. Otherwise, the first call returns -1 with errno set to EINPROGRESS. Subsequent calls return -1 with errno set to EALREADY while the connection is in progress and EISCONN after it is established.

For UDP sockets, connect() is always non-blocking. After a UDP socket is connected, send() may be used to send data to the remote peer. Unconnected UDP sockets must use sendto(), to specify the remote address. To prepare for sending data to a different remote peer, connect() can be used on a UDP socket that is already connected.

The socket’s local IP address and port number may either be specified by a prior bind() call or left unspecified, in which case connect() will assign an appropriate local IP address (that of an attached network interface) and an unused local port number. In the bind() call, either the IP address or port number may be left as zero for connect() to do the assignment.

If successful, connect() returns 0. Otherwise, it sets errno and returns -1.

ERROR CODES

EADDRINUSE The specified protocol, port number, and IP address is already in use.
EADDRNOTAVAIL The remote IP address is a broadcast or multicast address (TCP only).
EAFNOSUPPORT The address family of the remote address doesn’t match the socket’s.
EALREADY A connection request was previously sent and is not yet answered.
ECONNREFUSED The connection request was refused by the remote host.
EDESTADDRREQ Destination IP address is INADDR_ANY or destination port is zero.
EFAULT addr is NULL.
EHOSTUNREACH Timed out after receiving a “destination unreachable” ICMP message.
EINPROGRESS The call is non-blocking but has started the connection process.
EINVAL addrlen is less than the socket address size.
EISCONN The socket is already connected (TCP only).
ENETDOWN TargetTCP has not been initialized.
ENETUNREACH No route exists to destination network.
ENOBUFS Unable to allocate socket send/receive buffers (TCP only).
ENOTSOCK socket is not a valid socket handle.
EOPNOTSUPP socket is the handle of a socket in the listen state.
ETIMEDOUT connect() timed out without receiving a response to its SYN flag.

EXAMPLE

  struct sockaddr_in addr;

  /*-----------------------------------------------------------------*/
  /* Assign echo server address and connect.                         */
  /*-----------------------------------------------------------------*/
  bzero(&addr, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(ECHO_PORT);
  addr.sin_addr.s_addr = Server;
  if (connect(sock, &addr, sizeof(struct sockaddr_in)))
  {
    perror("Echo client connection failed");
    closesocket(sock);
    continue;
  }
  printf("Echo client connected\n");