Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

connect() - initiate a connection over a socket

&pagelevel(4)&pagelevel

#include <sys/socket.h>

int connect(int s, const struct sockaddr *name, size_t namelen);

Description

A process uses connect() to initiate communications with another process over a socket.

The s parameter designates the socket over which the process initiates communications with another process.

name is a pointer to the address of the communications partner. *name is an address in the communications domain of the socket to which the connection is to be initiated. Each communications domain interprets the name parameter in its own way.

namelen contains the length of the address of the communications partner in bytes.

The manner in which connect() proceeds differs according to whether the socket type is SOCK_STREAM or SOCK_DGRAM.

  • With a socket of type SOCK_STREAM (stream socket), connect() sends a connection request to a partner and tries in this way to set up a connection to this partner. The partner is specified with the name parameter. For example, a client process uses connect() to initiate a connection to a server over a stream socket.
    Stream sockets can generally only set up a connection with connect() once.

  • With a socket of type SOCK_DGRAM (datagram socket), a process uses connect() to define the name of the communications partner with which data is to be exchanged. The process then sends the datagrams to this communications partner. This communications partner is also the only socket from which the process can receive datagrams.
    With datagram sockets, connect() can be used several times to change the communications partner. The assignment to a specific partner can be terminated by entering a null pointer for the name parameter.

Return value

0:

If successful.

-1:

If errors occur. errno is set to indicate the error.

Errors

EADDRINUSE

The specified address is already in use.

EADDRNOTAVAIL

The specified address is invalid.

EAFNOSUPPORT

Addresses in the specified address family cannot be used with this socket.

EALREADY

This is a non-blocking socket and a previously sent connection request has not been concluded yet.

EBADF

s is not a valid descriptor.

ECONNREFUSED

The connection attempt has been successfully rejected. The calling program must close the socket descriptor with close() and request a new descriptor by recalling socket(). It can then use connect() to repeat the connection attempt.

EFAULT

The name parameter points to an invalid address.

EINTR

The connection setup attempt was interrupted by a signal.

EINVAL

The namelen parameter does not have the size of a valid address for the specified address family.

EISCONN

The socket already has a connection.

ENETUNREACH

The network is not reachable from this host.

ENETDOWN

The connection to the network is down.

ENOBUFS

Not enough resources to execute connect().

ENOTSOCK

Descriptor s references a file but not a socket.

ETIMEDOUT

The connection could not be set up within a specific time.

If the socket address family is AF_UNIX, executing connect() can also lead to an error for the following reasons:

EACCES

Access rights for a path name component were refused or write permission to the specified socket has been refused.

EDESTADDRREQ

The name parameter is the null pointer.

ENAMETOOLONG

A path name component exceeds NAME_MAX characters or the complete path name is longer than PATH_MAX characters.

ENOENT

A path name component refers to a non-existent file or the path name is blank.

ENOTDIR

A component in the path name is not a directory.

See also

accept(), getsockname(), socket(), close(), select()