connect() — Connect a socket
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2 |
both |
Format
#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/socket.h>
int connect(int socket, const struct sockaddr *address, socklen_t address_len);
#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.
- Parameter
- Description
- socket
- The socket descriptor.
- address
- The pointer to a socket address structure containing the address of the socket to which a connection will be attempted.
- address_len
- 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.
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.
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.
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.
Returned value
If successful, connect() returns 0.
- Error Code
- Description
- EADDRNOTAVAIL
- The specified address is not available from the local machine.
- EAFNOSUPPORT
- The address family is not supported.
- EALREADY
- The socket descriptor socket is marked nonblocking, and a previous connection attempt has not completed.
- EBADF
- The socket parameter is not a valid socket descriptor.
- ECONNREFUSED
- The connection request was rejected by the destination host.
- EFAULT
- 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.
- EINTR
- The attempt to establish a connection was interrupted by delivery of a signal that was caught. The connection will be established asynchronously.
- EINVAL
- The address_len parameter is not a valid length.
- EIO
- There has been a network or a transport failure.
- EISCONN
- The socket descriptor socket is already connected.
- ENETUNREACH
- The network cannot be reached from this host.
- ENOTSOCK
- The descriptor refers to a file, not a socket.
- EOPNOTSUPP
- The socket parameter is not of type SOCK_STREAM.
- EPERM
- 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.
- EPROTOTYPE
- The protocol is the wrong type for this socket.
- ETIMEDOUT
- The connection establishment timed out before a connection was made.
- Error Code
- Description
- EACCES
- Search permission is denied for a component of the path prefix, or write access to the named socket is denied.
- EIO
- An I/O error occurred while reading from or writing to the file system.
- ELOOP
- Too many symbolic links were encountered in translating the path name in address.
- ENAMETOOLONG
- A component of a path name exceeded NAME_MAX characters, or an entire path name exceeded PATH_MAX characters.
- ENOENT
- A component of the path name does not name an existing file or the path name is an empty string.
- ENOTDIR
- A component of the path prefix of the path name in address is not a directory.
Example
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("129.5.24.1");
/* specific interface */
inet_server.sin_port = htons(1024);
⋮
rc = connect(s, (struct sockaddr *) &inet_server,
sizeof(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));
Related information
- sys/socket.h
- accept() — Accept a new connection on a socket
- bind() — Bind a name to a socket
- htons() — Translate an unsigned short integer into network byte order
- inet_addr() — Translate an Internet address into network byte order
- listen() — Prepare the server for incoming client requests
- select(), pselect() — Monitor activity on files or sockets and message queues
- selectex() — Monitor activity on files or sockets and message queues
- socket() — Create a socket