connect
connect establishes a connection with another host.
Syntax
int connect(
SOCKET s,
const struct sockaddr *name,
int namelen
);
Parameters
s
A socket descriptor for a socket created by socket.
name
A pointer to the sockaddr structure with the endpoint address (IP address and a port) of the host to connect to..
namelen
The length of the endpoint address in bytes.
Return Value
If the function succeeds, it returns 0 (zero). If the function fails, it returns SOCKET_ERROR.
Use WSAGetLastError to retrieve a specific error code.
Remarks
For TCP sockets, connect attempts to connect to the specified destination using a 3-way handshake. connect on a blocking socket returns when the connection is established or fails to connect.
A non-blocking socket becomes ready to write when the connection has been established or has failed, which can be checked by the select function call with the socket descriptor in the write descriptors set.
There are significant differences in the way non-blocking connect is handled between the eRTOS TCP/IP Stack and Microsoft Windows, as noted below.
- If the connection has not completed in a single connect call, the non-blocking connect sets an error code WSAEINPROGRESS in the RTSS application, not WSAEWOULDBLOCK.
- If the connection error occurs when an RTSS application uses the non-blocking connect, both read and write descriptor sets are notified and the exception set does not get notified. This is because the eRTOS TCP/IP Stack uses the exception descriptor set only for out-of-band TCP data notification.
- The recv call with MSG_PEEK flag can be used with select to know whether the connection has been completed or failed. If the connection is completed, the recv will succeed or fail with error code WSAEWOULDBLOCK. If the connection has failed, the error code, retrieved by WSAGetLastError, indicates the reason for the failed connection.
For a connectionless socket, no datagrams are exchanged. Instead, connect specifies a destination for subsequent sends on the socket, and restricts the socket to receiving datagrams from the specified remote address.
Setting Scope ID is necessary when connecting to a link-local IPv6 address; otherwise, connect will fail.
connect is not supported for SOCK_RAW type sockets.
Requirements
Minimum supported version | Header | Library |
---|---|---|
eRTOS 1.0 SDK |
Winsock2.h | RtTcpipApi.lib |