connect(3sock)


connect -- initiate a connection on a socket

Synopsis

cc [options] file -lsocket -lnsl
#include <sys/types.h>
#include <sys/socket.h>

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

Description

connect requests that a connection be made on a socket. If the initiating socket is not connection-oriented, then connect sets the socket's peer address but no connection is made. For SOCK_DGRAM sockets, the peer address identifies where all datagrams are sent on subsequent send(3sock) calls, and limits the remote sender for subsequent recv(3sock) calls. If address is a null address for the protocol, the socket's peer address will be reset.

If the initiating socket is connection-oriented, then connect attempts to establish a connection to the address specified by address.

If the connection cannot be established immediately and O_NONBLOCK is not set for the file descriptor for the socket, connect will block for up to an unspecified timeout interval until the connection is established. If the timeout interval expires before the connection is established, connect will fail and the connection attempt will be aborted. If connect is interrupted by a signal that is caught while blocked waiting to connect, connect will fail and set errno to EINTR, but the connection request will not be aborted, and the connection will be established asynchronously.

If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect will fail and set errno to EINPROGRESS, but the connection request will not be aborted, and the connection will be established asynchronously. Subsequent calls to connect for the same socket, before the connection is established, will fail and set errno to EALREADY.

When the connection has been established asynchronously, select(3C) and poll(2) will indicate that the file descriptor for the socket is ready for writing. However, this is only possible if the socket STREAMS module is the topmost module on the protocol stack with a write service procedure. This will be the normal case.

Files

/usr/lib/locale/locale/LC_MESSAGES/uxns1

Return values

If the connection or binding succeeds, then 0 is returned. Otherwise a -1 is returned and a more specific error code is stored in errno.

Errors

The call fails if:

EADDRNOTAVAIL
The specified address is not available from the local machine.

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

EALREADY
A connection request is already in progress for the specified socket.

EBADF
socket is not a valid descriptor.

ECONNREFUSED
The attempt to connect was refused or the target address was not listening for connections.

EINPROGRESS
The socket is non-blocking and the connection cannot be completed immediately; the connection will be established asynchronously.

EINTR
The connection attempt was interrupted before any data arrived by the delivery of a signal. The connection will be established asynchronously.

EISCONN
The socket is connection-oriented, and is already connected.

ENETUNREACH
The network is not reachable from this host.

ENOTSOCK
socket does not refer to a socket.

EPROTOTYPE
The file referred to by address is a socket of a type other than the socket bound to the specified peer address.

ETIMEDOUT
Connection establishment timed out without establishing a connection.

EADDRINUSE
The address is already in use.

ECONNRESET
The remote host reset the connection request.

EINVAL
address_len is not the size of a valid address for the specified address family, or invalid address family in sockaddr structure.

ENAMETOOLONG
Pathname resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX.

ENETDOWN
The local interface used to reach the destination is down.

ENOBUFS
No buffer space is available.

ENOSR
There were insufficient STREAMS resources available to complete the operation.
If the address family of the socket is AF_UNIX, connect will fail if:

ENOTDIR
A component of the path prefix of the pathname in address is not a directory.

ENAMETOOLONG
A component of a pathname exceeded NAME_MAX characters or an entire pathname exceeded PATH_MAX.

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 pathname in address.

ENOENT
A component of the pathname does not name an existing file or the pathname is an empty string.

References

accept(3sock), close(2), getsockname(3sock), socket(3sock)

RFC 2133

Notices

The behavior of non-blocking connect for unix domain (AF_UNIX) sockets is determined by the value of the ss_connafunixndelay parameter set by inconfig(1Mtcp).

The type of address structure passed to connect depends on the address family. UNIX® domain sockets (address family AF_UNIX) require a socketaddr_un structure as defined in sys/un.h; Internet domain IPv4 sockets (address family AF_INET) require a sockaddr_in structure as defined in netinet/in.h; Internet domain IPv6 sockets (address family AF_INET6) require a sockaddr_in6 structure as defined in netinet/in.h. Other address families may require other structures. Use the structure appropriate to the address family; cast the structure address to a generic struct sockaddr * in the call to connect and pass the size of the structure in the length argument. If connect fails, the state of the socket is unspecified. Portable applications should close the file descriptor and create a new socket before attempting to reconnect.

In UnixWare 7 the sockaddr structure has been modified to support variable length sockets. The net result of this modification is that the family member has been shortened to 8 bits and a new 8-bit member inserted before it called len. For more information on the new sockaddr structures, see: unix(7sock) and inet(7tcp).


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004