connect
connect establishes a connection to a specified socket.
Syntax
int connect(
[in] SOCKET s,
[in] const struct sockaddr *name,
[in] int namelen
);
Parameters
[in] s
The socket descriptor created by socket.
[in] name
A pointer to the sockaddr structure with the host's endpoint address (IP address and a port) to establish a connection to.
[in] namelen
The network address length, in bytes, stored in the name parameter.
Return Value
If the function succeeds, it returns 0 (zero). If the function fails, it returns SOCKET_ERROR. Call 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 of failure.
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.
As noted below, the TCP/IP Stack and Microsoft Windows handle non-blocking connections differently.
- 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 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 in conjunction with select to know whether the connection has completed or failed. If the connection has been 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 |
|---|---|---|
|
wRTOS 1.0 SDK |
winsock2.h |
RttcpipApi.lib |