connect

connect establishes a connection with another host.

Syntax

int connect(
    SOCKET s,
    const struct sockaddr *name,
    int namelen
);

Parameters

s

Socket descriptor for a socket created by socket.

name

Pointer to the sockaddr structure with the endpoint address (IP address and a port) of the host to establish a connection to.

namelen

Length of the endpoint address in bytes.

Return Value

0 (zero) if the function succeeds, SOCKET_ERROR if the function fails

Use WSAGetLastError to retrieve a specific error code.

Remarks

For TCP sockets, connect attempts to establish a connection to the specified destination using a 3-way handshake. connect on a blocking socket returns when the connection is established of failed.

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 RT-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 RTX64 2013
Header Winsock2.h
Library RtTcpip.lib