SockConnect

The SockConnect() socket call requests a connection to a remote host.

Syntax:

>>--SockConnect(socket, address)-----------------------------------------><

where:

socket

is the socket descriptor used to issue the connection request.

address

is a stem variable containing the address of the socket to which a connection is to be established.

The SockConnect() call performs the following tasks when called for a stream socket:

  1. It completes the binding for a socket, if necessary.

  2. It attempts to create a connection between two sockets.

This call is used by the client side of socket-based applications to establish a connection with a server. The remote server must have a passive open pending, which means it must successfully call SockBind() and SockListen(). Otherwise, SockConnect() returns the value -1 and the error value is set to ECONNREFUSED.

In the Internet communication domain, a timeout occurs if a connection to the remote host is not established within 75 seconds.

If the socket is in blocking mode, the SockConnect() call blocks the caller until the connection is established or an error is received. If the socket is in nonblocking mode, SockConnect() returns the value -1 and sets the error value to EINPROGRESS if the connection was successfully initiated. The caller can test the completion of the connection by calling:

Stream sockets can call SockConnect() only once.

Datagram or raw sockets normally transfer data without being connected to the sender or receiver. However, an application can connect to such a socket by calling SockConnect(). SockConnect() specifies and stores the destination peer address for the socket. The system then knows to which address to send data and the destination peer address does not have to be specified for each datagram sent. The address is kept until the next SockConnect() call. This permits the use of the SockRecv() and SockSend() calls, which are usually reserved for connection-oriented sockets. However, data is still not necessarily delivered, which means the normal features of sockets using connectionless data transfer are maintained. The application can therefore still use the SockSendTo()and SockRecvFrom() calls.

Datagram and raw sockets can call SockConnect() several times. The application can change their destination address by specifying a new address on the SockConnect() call. In addition, the socket can be returned to a connectionless mode by calling SockConnect() with a null destination address. The null address is created by setting the stem variable address as follows: the family field to AF_INET, the port field to 0, and the addr field to 0.0.0.0.

The call to SockConnect returns the value -1, indicating that the connection to the null address cannot be established. Calling SockSock_Errno() returns the value EADDRNOTAVAIL.

Return values:

The value 0 indicates successful execution of the call. The value -1 indicates an error. You can get the specific error code by calling SockSock_Errno() or SockPSock_Errno(). Possible values are:

EADDRNOTAVAIL

The calling host cannot reach the specified destination.

EAFNOSUPPORT

The address family is not supported.

EALREADY

The socket is in nonblocking mode. A previous connection attempt has not completed.

ENOTSOCK

The socket is not a valid socket descriptor.

ECONNREFUSED

The destination host rejected the connection request.

EINPROGRESS

socket is in nonblocking mode, and the connection cannot be completed immediately. EINPROGRESS does not indicate an error.

EINTR

Interrupted system call.

EISCONN

socket is already connected.

ENETUNREACH

The network cannot be reached from this host.

ETIMEDOUT

Establishing the connection timed out.

ENOBUFS

There is no buffer space available.

EOPNOTSUPP

The operation is not supported on socket.

Note: SockConnect interfaces with the C function connect().