connect() — Connect a socket


Standards / Extensions C or C++ Dependencies

Single UNIX Specification, Version 3



#include <sys/socket.h>

int connect(int socket, const struct sockaddr *address, socklen_t address_len);
Berkeley sockets:
#define _OE_SOCKETS
#include <sys/types.h>
#include <sys/socket.h>

int connect(int socket, struct sockaddr *address, int address_len);

General description

For stream sockets, the connect() call attempts to establish a connection between two sockets. For datagram sockets, the connect() call specifies the peer for a socket. The socket parameter is the socket used to originate the connection request. The connect() call performs two tasks when called for a stream socket. First, it completes the binding necessary for a stream socket (in case it has not been previously bound using the bind() call). Second, it attempts to make a connection to another socket.

Note: For the X/Open socket function, the socket description applies to socket, address to address, and address_len to address_len. const is added to struct sockaddr.
The socket descriptor.
The pointer to a socket address structure containing the address of the socket to which a connection will be attempted.
The size of the socket address pointed to by address in bytes.

The connect() call on a stream socket is used by the client application to establish a connection to a server. The server must have a passive open pending. A server that is using sockets must successfully call bind() and listen() before a connection can be accepted by the server with accept(). Otherwise, connect() returns -1 and the error code is set to ECONNREFUSED.

If socket is in blocking mode, the connect() call blocks the caller until the connection is set up, or until an error is received. If socket is in non-blocking mode, the connect returns immediately with a return code of -1 and an errno of EINPROGRESS. The caller can test the completion of the connection setup by calling select() and testing for the ability to write to the socket.

When called for a datagram socket, connect() specifies the peer with which this socket is associated. This gives the application the ability to use data transfer calls reserved for sockets that are in the connected state. In this case, read(), write(), readv(), writev(), send(), and recv() calls are then available in addition to sendto(), recvfrom(), sendmsg(), and recvmsg() calls. Stream sockets can call connect() only once, but datagram sockets can call connect() multiple times to change their association. Datagram sockets can dissolve their association by connecting to an incorrect address, such as the NULL address (all fields zeroed).

The address parameter is a pointer to a buffer containing the name of the peer to which the application needs to connect. The address_len parameter is the size, in bytes, of the buffer pointed to by address.

Servers in the AF_INET domain: If the server is in the AF_INET domain, the format of the name buffer is expected to be sockaddr_in, as defined in the include file netinet/in.h.
struct in_addr
        ip_addr_t s_addr;

 struct sockaddr_in {
     unsigned char  sin_len;
     unsigned char  sin_family;
     unsigned short sin_port;
     struct in_addr sin_addr;
     unsigned char  sin_zero[8];


The sin_family field must be set to AF_INET. The sin_port field is set to the port to which the server is bound. It must be specified in network byte order. The sin_zero field is not used and must be set to all zeros.

Servers in the AF_INET6 domain: If the server is in the AF_INET6 domain, the format of the name buffer is expected to be sockaddr_in6, as defined in the netinet/in.h:
 struct sockaddr_in6 {
     uint8_t char    sin6_len;
     sa_family_t     sin6_family;
     in_port_t       sin6_port;
     uint32_t        sin6_flowinfo;
     struct in6_addr sin6_addr;
     uint32_t        sin6_scope_id;

The sin6_family must be set to AF_INET6.

Servers in the AF_UNIX domain: If the server is in the AF_UNIX domain, the format of the name buffer is expected to be sockaddr_un, as defined in the include file un.h.
 struct sockaddr_un {
    unsigned char  sun_len;
    unsigned char  sun_family;
             char  sun_path[108];        /* path name */

The sun_family field is set to AF_UNIX. The sun_path field contains the NULL-terminated path name, and sun_len contains the length of the path name.

Special behavior for C++: To use this function with C++, you must use the _XOPEN_SOURCE_EXTENDED 1 feature test macro.
Note: The connect() function has a dependency on the level of the Enhanced ASCII Extensions. See Enhanced ASCII support for details.

Returned value

If successful, connect() returns 0.

If unsuccessful, connect() returns -1 and sets errno to one of the following values:
Error Code
The specified address is not available from the local machine.
The address family is not supported.
The socket descriptor socket is marked nonblocking, and a previous connection attempt has not completed.
The socket parameter is not a valid socket descriptor.
The connection request was rejected by the destination host.
Using address and address_len would result in an attempt to copy the address into a portion of the caller's address space to which data cannot be written.
The attempt to establish a connection was interrupted by delivery of a signal that was caught. The connection will be established asynchronously.
The address_len parameter is not a valid length.
There has been a network or a transport failure.
The socket descriptor socket is already connected.
The network cannot be reached from this host.
The descriptor refers to a file, not a socket.
The socket parameter is not of type SOCK_STREAM.
connect() caller was attempting to extract a user's identity and the caller's process was not verified to be a server. To be server-verified, the caller's process must have permission to the BPX.SERVER profile (or superuser and BPX.SERVER is undefined) and have called either the __passwd() or pthread_security_np() services before calling connect() to propagate identity.
The protocol is the wrong type for this socket.
The connection establishment timed out before a connection was made.
The following are for AF_UNIX only:
Error Code
Search permission is denied for a component of the path prefix, or write access to the named socket is denied.
An I/O error occurred while reading from or writing to the file system.
Too many symbolic links were encountered in translating the path name in address.
A component of a path name exceeded NAME_MAX characters, or an entire path name exceeded PATH_MAX characters.
A component of the path name does not name an existing file or the path name is an empty string.
A component of the path prefix of the path name in address is not a directory.


The following are examples of the connect() call. The Internet address and port must be in network byte order. To put the port into network byte order, the htons() utility routine is called to convert a short integer from host byte order to network byte order. The address field is set using another utility routine, inet_addr(), which takes a character string representing the dotted-decimal address of an interface and returns the binary Internet address representation in network byte order. Finally, it is a good idea to zero the structure before using it to ensure that the name requested does not set any reserved fields. These examples could be used to connect to the servers shown in the examples listed with the call, bind() — Bind a name to a socket.
int s;
struct sockaddr_in inet_server;
struct sockaddr_un unix_server;
int rc;
int connect(int s, struct sockaddr *name, int namelen);

/* Connect to server bound to a specific interface in the 
Internet domain */
/* make sure the sin_zero field is cleared */
memset(&inet_server, 0, sizeof(inet_server));
inet_server.sin_family = AF_INET;
inet_server.sin_addr.s_addr = inet_addr(""); 
/* specific interface */
inet_server.sin_port = htons(1024);
rc = connect(s, (struct sockaddr *) &inet_server, 

/* Connect to a server bound to a name in the UNIX domain */
/* make sure the sunix_addr, sunix_port, sunix_nodeid fields are cleared
memset(&unix_server, 0, sizeof(unix_server));
unix_server.sun_family = AF_UNIX;
strncpy(unix_server.sun_path, "mysocket");
unix_server.sun_len = sizeof(unix_server.sun_len);
strncpy(mvsservername.sunix_name, "APPL    ", 8);
rc = connect(s, (struct sockaddr *) 
&unix_server, sizeof(unix_server));