HPlogo Berkeley Sockets/iX Reference Manual: HP 3000 MPE/iX Computer Systems > Chapter 2 Socket System Calls

CONNECT

» 

Technical documentation

Complete book in PDF
» Feedback

 » Table of Contents

C Interface

   AF_UNIX sockets only:



        #include <sys/types.h>

        #include <sys/socket.h>

        #include <sys/un.h>



        connect(s, addr, addrlen)

        int s;

        struct sockaddr_un *addr;

        int addrlen;



   AF_INET sockets only:



        #include <sys/types.h>

        #include <sys/socket.h>

        #include <netinet/in.h>



        connect(s, addr, addrlen)

        int s;

        struct sockaddr_in *addr;

        int addrlen;

Description

The parameter s is a socket descriptor. The addr parameter is a pointer to a socket address structure. This structure contains the address of a remote socket to which a connection is established. The addrlen parameter specifies the size of this address structure. Since the size of the socket address structure varies between socket address families (16 bytes for AF_INET; 110 bytes for AF_UNIX), the correct socket address structure should be used with each address family (struct sockaddr_in for AF_INET, struct sockaddr_un for AF_UNIX).

If the socket is of type SOCK_STREAM, then connect attempts to contact the remote host in order to make a connection between the remote socket (peer) and the local socket specified by s. The call normally blocks until the connection completes. If non-blocking mode has been enabled using the O_NONBLOCK or O_NDELAY fcntl flags and the connection cannot be completed immediately, then connect returns an error as described below. In these cases, the select call can be used on this socket to determine when the connection has completed by selecting it for writing.

The O_NONBLOCK and O_NDELAY flags are defined in <fcntl.h> and are explained in the fcntl section. If s is a SOCK_STREAM socket that is bound to the same local address as another SOCK_STREAM socket and addr is the same as the peer address of the other socket, connect returns EADDRINUSE.

If the AF_INET socket does not already have a local address bound to it (refer to the bind call), the connect call also binds the socket to a local address chosen by the system.

Stream sockets may successfully connect only once.

Return Value

If the call is successful, a 0 is returned. If it fails, a -1 is returned, and an error code is stored in errno.

Errors

The following errors are returned by connect:

[EBADF]

The argument s is not a valid file descriptor.

[ENOTSOCK]

The argument s is a file descriptor for a file, not a socket.

[EADDRNOTAVAIL]

The specified address is not available on this machine, or the socket is a tcp or udp socket and the zero port number is specified.

[EAFNOSUPPORT]

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

For datagram sockets, the peer address is no longer maintained by the system.

[EISCONN]

The socket is already connected.

[EINVAL]

The socket has already been shut down or has a listen active on it, or addrlen is a bad value.

[ETIMEDOUT]

Connection establishment timed out without establishing a connection. The backlog parameter may be full. (Refer to the listen call.)

[ECONNREFUSED]

The attempt to connect was forcefully rejected.

[ENETUNREACH]

The network is not reachable from this host.

[EADDRINUSE]

The address is already in use.

[EFAULT]

The addr parameter is not a valid pointer.

[EINPROGRESS]

Non-blocking I/O is enabled using O_NONBLOCK or O_NDELAY, and the connection cannot be completed immediately. This is not a failure.

[ENOSPC]

All available virtual circuits are in use.

[EOPNOTSUPP]

A connect attempt was made on a socket type that does not support this call.

[EINTR]

The call was interrupted by a signal before a valid connection arrived.

MPE/iX Specific

The connect call is not supported for SOCK_DGRAM sockets on the current release.

Author

UCB (University of California at Berkeley)

See Also

accept, select, socket, getsockname, fcntl

Feedback to webmaster