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).
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 informationServers 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.
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.
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.
ExampleFollowing 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().
Related callsbind(), htons(), inet_addr(), listen(), select(), selectex(), socket() |
Copyright IBM Corporation 1990, 2014
|