connect

connect establishes a connection with another host.

Syntax

Copy
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.

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