CONNECT
A client application uses the CONNECT command to establish a connection between a local socket and a remote socket.
The command supports both blocking and nonblocking sockets. When the socket is in blocking mode, the function does not return until a connection with the remote peer is established or until an error is received. When the socket is in nonblocking mode, the function returns immediately with either the 36 EINPROGRESS return code or an error.
- Stream (TCP) sockets
- If the application has not already issued an explicit bind, the CONNECT command completes the bind of the socket. The API then attempts to establish a connection to the remote socket that is specified by the name parameter. You can call the CONNECT command only once. Issuing additional CONNECT commands results in a 56 EISCONN error.
- Datagram (UDP) sockets
- The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.
- Using the CONNECT command on a UDP socket does not change the UDP protocol from a connectionless to a connection-based protocol. The UDP socket remains connectionless. The primary benefit of using connected UDP sockets is to limit communication with a single remote application.
- When a UDP socket becomes a connected UDP socket, it can no longer use the SENDTO and RECVFROM commands. Connected UDP sockets use the socket commands READ, WRITE, SEND, or RECV to communicate with the remote peer, instead of using the SENTO and RECVFROM commands.
- For nonblocking sockets, use the SELECT command to determine when a connection has been established. Test for the ability to write to the socket.
- A connected UDP socket can revert back to an unconnected UDP socket by calling CONNECT with 0 or AF_UNSPEC specified in the domain field of the name parameter.
Format
Parameters
- socketid
- The descriptor of the local socket.
- name
- Identifies the remote socket. The format for the name parameter depends on the socket type:
- AF_INET sockets (IPv4)
- name = "domain portid ipaddress"
- AF_INET6 sockets (IPv6)
- name = "domain portid flowinfo ipaddress scopeid"
- The domain value is the decimal number 2 for AF_INET and the decimal number 19 for AF_INET6.
- The portid value is the port number.
- The ipaddress value is the IP address of the remote host. It must be an IPv4 address for AF_INET and an IPv6 address for AF_INET6.
- The flowinfo value must be 0.
- The scopeid value identifies the interfaces that are applicable for the scope of the address that is specified in the ipaddress field. For a link-local IP address, the scopeid field can specify a link index, which identifies a set of interfaces. For all other scopes, the scopeid field must be set to 0. Setting the scopeid field to 0 indicates that any address type and scope can be specified.
Returned value
The return code can be 0, a REXX socket API error number, or the REXX TCP/IP error number that is set by the socket command. The return code 0 indicates that the requested socket command was completed successfully.
See Socket call error return codes for additional information about the numeric error codes that are returned by this command.
- 1 EPERM
- 9 EBADF
- 35 EWOULDBLOCK
- 36 EINPROGRESS
- 37 EALREADY
- 47 EAFNOSUPPORT
- 48 EADDRINUSE
- 49 EADDRNOTAVAIL
- 51 ENETUNREACH
- 56 EISCONN
- 60 ETIMEDOUT
- 61 ECONNREFUSED
- 2001 EINVALIDRXSOCKETCALL
- 2009 ESOCKETNOTDEFINED
LE C/C++ equivalent
int connect(int socket, struct sockaddr *address, int address_len);