connect
Purpose
Initiates a connection on a socket.
Library
Sockets Library (libsock.a)
Syntax
#include <sys/types.h>
#include <sys/socket.h>
int connect (s, name, namelen)
int s;
struct sockaddr *name;
int namelen;
Description
The s parameter is a socket. If it is of type
SOCK_DGRAM, then this subroutine permanently specifies
the peer, the socket to which datagrams are sent. This
allows use of the send subroutine rather than sendto or
sendmsg, which require the address to which the socket
data should be sent.
If the socket is of type SOCK_STREAM, which provides a
full-duplex data connection between two sockets, this
subroutine attempts to make a connection to another
socket. The other socket is specified by name, which is
an address in the communications space of the socket.
Each communications space interprets the name parameter
in its own way.
Return Value
Upon successful completion, a value of 0 is returned. If
the connect routine fails, a value of -1 is returned, and
errno is set to indicate the error.
Diagnostics
The subroutine fails if one or more of the following are
true:
EBADF The s parameter is not valid.
ENOTSOCK The s parameter refers to a file, not a
socket.
EADDRNOTAVAIL The specified address is not available
from the local machine.
EAFNOSUPPORT The addresses in the specified address
family cannot be used with this socket.
EISCONN The socket is already connected.
ETIMEDOUT The establishment of a connection timed
out before a connection was made.
ECONNREFUSED The attempt to connect was rejected.
ENETUNREACH The network is not reachable from this
host.
EADDRINUSE The specified address is already in
use.
EFAULT The addr parameter is not in a writable
part of the user address space.
EWOULDBLOCK The socket is marked non-blocking, and
the connection cannot be immediately
completed. You can select the socket
for writing during the connection
process.
Related Information
In this book: "accept," "getsockname," "select," and
"socket."