z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


connect()

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

For stream sockets, the connect() call attempts to establish a connection between two sockets. For UDP sockets, the connect() call specifies the peer for a socket. The s 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 connect to another socket.

The connect() call on a stream socket is used by the client application to connect to a server. The server must have a passive open pending. If the server is using sockets, this means the server must successfully call bind() and listen() before a connection can be accepted by the server using accept(). Otherwise, connect() returns -1 and errno is set to ECONNREFUSED.

If s is in blocking mode, the connect() call blocks the caller until the connection is set up, or until an error is received. If the socket is in nonblocking mode, then connect() returns -1 with errno set to EINPROGRESS if the connection can be initiated (no other errors occurred). The caller can test completion of the connection setup by calling select() and testing ability to write to the socket.

When called for a datagram or raw 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 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 a null address (all fields cleared).

#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
#include <in.h>
int connect(int s, struct sockaddr *name, int namelen)
Parameter
Description
s
Socket descriptor
name
Points to a socket address structure containing the address of the socket to which connection will be attempted
namelen
Size of the socket address, in bytes, pointed to by name

The name parameter points to a buffer containing the name of the peer to which the application needs to connect. The namelen parameter is the size, in bytes, of the buffer pointed to by name.

Related information

Servers in the AF_INET domain

If the server is in the AF_INET domain, the format of the name buffer is expected to be sockaddr_in as defined in the header file IN.H.
struct in_addr
{
        u_long s_addr;
};
struct sockaddr_in
{
        short   sin_family;
        u_short sin_port;
        struct  in_addr sin_addr;
        char    sin_zero[8];
};

The sin_family field must be set to AF_INET. The sin_port field identifies the port to which the server is bound; it must be specified in network byte order. The sin_addr field specifies a 32–bit Internet address. The sin_zero field is not used, and must be set to all zeros.

Servers in the AF_IUCV domain

If the server is in the AF_IUCV domain, the format of the name buffer is expected to be sockaddr_iucv as defined in the header file SAIUCV.H.
struct sockaddr_iucv
{
    short           siucv_family;    /* addressing family */
    unsigned short  siucv_port;      /* port number */
    unsigned long   siucv_addr;      /* address */
    unsigned char   siucv_nodeid[8]; /* nodeid to connect to */
    unsigned char   siucv_userid[8]; /* userid to connect to */
    unsigned char   siucv_name[8];   /* iucvname for connect */
};
The siucv_family field must be set to AF_IUCV.
Note: The siucv_port, siucv_addr, and siucv_nodeid fields are reserved for future use.
The siucv_port and siucv_addr fields must be set to 0. Set the siucv_nodeid field to exactly eight blank characters. The siucv_userid field is set to the MVS™ user ID of the application to which the application is requesting a connection. This field must be eight characters long, padded with blanks to the right. It cannot contain the null character. The siucv_name field is set to the application name by which the server socket is known. The name should exactly match the eight characters passed in the bind() call executed by the server.

Return values

The value 0 indicates success; the value -1 indicates an error. Errno identifies the specific error.
Errno
Description
EADDRNOTAVAIL
Calling host cannot reach the specified destination.
EAFNOSUPPORT
Address family is not supported.
EALREADY
Socket descriptor s is marked nonblocking, and a previous connection attempt is incomplete.
EBADF
The s parameter is not a valid socket descriptor.
ECONNREFUSED
The connection request was rejected by the destination host.
EFAULT
The name or namelen parameter specified an address outside of the caller address space.
EINPROGRESS
The socket descriptor s is marked nonblocking, and the connection cannot be completed immediately. The EINPROGRESS value does not indicate an error.
EISCONN
Socket descriptor s is already connected.
ENETUNREACH
Network cannot be reached from this host.
ETIMEDOUT
Connection attempt timed out before the connection was made.

Example

Following is a connect() call example. The internet address and port must be in network byte order. To put the port into network byte order, the htons() utility is called to convert a short integer from host byte order to network byte order. The address field is set using another utility, inet_addr(), which takes a character string representing the dotted decimal address of an interface and returns the binary internet address in network byte order. Set the structure to 0 before using it to ensure that the name requested does not set any reserved fields.

These examples could be used to connect to the servers shown in the examples listed with the call bind().
int s;
struct sockaddr_in servername;
struct sockaddr_iucv mvsservername;
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(&servername, 0, sizeof(servername));
servername.sin_family = AF_INET;
servername.sin_addr = inet_addr(“129.5.24.1”); /* specific interface */
servername.sin_port = htons(1024);
⋮
rc = connect(s, (struct sockaddr *) &servername, sizeof(servername));
/* Connect to a server bound to a name in the IUCV domain */
/* make sure the siucv_addr, siucv_port, siucv_nodeid fields are cleared
*/
memset(&mvsservername, 0, sizeof(mvsservername));
mvsservername.siucv_family = AF_IUCV;
strncpy(mvsservername.siucv_nodeid, “        ”,8);
/* The field is 8 positions padded to the right with blanks */
strncpy(mvsservername.siucv_userid, “MVSUSER1 ”, 8);
strncpy(mvsservername.siucv_name, “APPL    ”, 8);
⋮
rc = connect(s, (struct sockaddr *) &mvsservername, sizeof(mvsservername));

Related calls

bind(), htons(), inet_addr(), listen(), select(), selectex(), socket()

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014