WSAConnectByNameW

The WSAConnectByName function establishes a connection to a specified host and port. This function is provided to allow a quick connection to a network endpoint given a host name and port.

This function supports both IPv4 and IPv6 addresses.

Syntax

BOOL WSAConnectByNameW(
         SOCKET s,
         LPWSTR nodename,
         LPWSTR servicename,
         LPDWORD LocalAddressLength,
         LPSOCKADDR LocalAddress,
         LPDWORD RemoteAddressLength,
         LPSOCKADDR RemoteAddress,
         const timeval *timeout,
         _Reserved_ LPWSAOVERLAPPED Reserved
)  

Parameters

s
Type: SOCKET 

[in]

A descriptor that identifies an unconnected socket.

Note

  On Windows 7, Windows Server 2008 R2, and earlier, the WSAConnectByName function requires an unbound and unconnected socket. This differs from other Winsock calls to establish a connection (for example, WSAConnect).

nodename
Type: LPWSTR 

[in] A NULL-terminated string that contains the name of the host or the IP address of the host on which to connect for IPv4 or IPv6.

servicename
Type: LPWSTR 

[in]

A NULL-terminated string that contains the service name or destination port of the host on which to connect for IPv4 or IPv6.

A service name is a string alias for a port number. For example, “http” is an alias for port 80 defined by the Internet Engineering Task Force (IETF) as the default port used by web servers for the HTTP protocol. Possible values for the servicename parameter when a port number is not specified are listed in the following file:
%WINDIR%\system32\drivers\etc\services

LocalAddressLength
Type: LPDWORD 

[in, out, optional] On input, a pointer to the size, in bytes, of the LocalAddress buffer provided by the caller. On output, a pointer to the size, in bytes, of the SOCKADDR for the local address stored in the LocalAddress buffer filled in by the system upon successful completion of the call.

LocalAddress
Type: LPSOCKADDR 

[out, optional] A pointer to the SOCKADDR structure that receives the local address of the connection. The size of the parameter is exactly the size returned in LocalAddressLength. This is the same information that would be returned by the getsockname function. This parameter can be NULL, in which case, the LocalAddressLength parameter is ignored.

RemoteAddressLength
Type: LPDWORD 

[in, out, optional] On input, a pointer to the size, in bytes, of the RemoteAddress buffer provided by the caller. On output, a pointer to the size, in bytes, of the SOCKADDR for the remote address stored in RemoteAddress buffer filled-in by the system upon successful completion of the call.

RemoteAddress
Type: LPSOCKADDR 

[out, optional] A pointer to the SOCKADDR structure that receives the remote address of the connection. This is the same information that would be returned by the getpeername function. This parameter can be NULL, in which case, the RemoteAddressLength is ignored.

*timeout
Type: timeval 

[in, optional] The time, in milliseconds, to wait for a response from the remote application before aborting the call.

Reserved
Type: Reserved LPWSAOVERLAPPED 

Reserved for future implementation. This parameter must be set to NULL.

Return value

Type: BOOL 

If a connection is established, WSAConnectByName returns TRUE and LocalAddress and RemoteAddress parameters are filled in if these buffers were supplied by the caller.

If the call fails, FALSE is returned. WSAGetLastError can then be called to get extended error information.

Return codes
Return code Description
WSAEHOSTUNREACH The host passed as the nodename parameter was unreachable.
WSAEINVAL An invalid parameter was passed to the function. The nodename or the servicename parameter must not be NULL. The Reserved parameter must be NULL.
WSAENOBUFS Sufficient memory could not be allocated.
WSAENOTSOCK An invalid socket was passed to the function. The s parameter must not be INVALID_SOCKET or NULL.
WSAETIMEDOUT A response from the remote application was not received before the timeout parameter was exceeded.

Remarks

WSAConnectByName is provided to enable quick and transparent connections to remote hosts on specific ports. It is compatible with both IPv6 and IPv4 versions.

To enable both IPv6 and IPv4 communications, use the following method:

WSAConnectByName has limitations: It works only for connection-oriented sockets, such as those of type SOCK_STREAM. The function does not support overlapped I/O or non-blocking behavior. WSAConnectByName will block even if the socket is in non-blocking mode.

WSAConnectByName does not support user-provided data during the establishment of a connection. This call does not support FLOWSPEC structures, either. In cases where these features are required, WSAConnect must be used instead.

If an application needs to bind to a specific local address or port, then WSAConnectByName cannot be used since the socket parameter to WSAConnectByName must be an unbound socket.

The RemoteAddress and the LocalAddress parameters point to a SOCKADDR structure, which is a generic data type. When WSAConnectByName is called, it is expected that a socket address type specific to the network protocol or address family being used will actually be passed in these parameters. So for IPv4 addresses, a pointer to a sockaddr_in structure would be cast to a pointer to SOCKADDR as the RemoteAddress and LocalAddressparameters. For IPv6 addresses, a pointer to a sockaddr_in6 structure would be cast to a pointer to SOCKADDR as the RemoteAddress and LocalAddressparameters.

When the WSAConnectByName function returns TRUE, the socket s is in the default state for a connected socket. The socket s does not enable previously set properties or options until SO_UPDATE_CONNECT_CONTEXT is set on the socket. Use the setsockopt function to set the SO_UPDATE_CONNECT_CONTEXT option.

Example

For example:

Note

When issuing a blocking Winsock call such as WSAConnectByName with the timeout parameter set to NULL, Winsock may need to wait for a network event before the call can complete. Winsock performs an alertable wait in this situation, which can be interrupted by an asynchronous procedure call (APC) scheduled on the same thread. Issuing another blocking Winsock call inside an APC that interrupted an ongoing blocking Winsock call on the same thread will lead to undefined behavior, and must never be attempted by Winsock clients.

Windows Phone 8: This API is supported.

Requirements

Header: Declared in winsock2.h.

Library: Use ws2_32.lib.