The Windows Sockets connect
function establishes a connection to a peer.
int connect (
| SOCKET s,
| const struct sockaddr FAR* name,
| int namelen
[in] A descriptor identifying an unconnected socket.
[in] The name of the peer to which the socket is to be connected.
[in] The length of the name
This function is used to create a connection to the specified destination. If
the socket, s
, is unbound, unique values are assigned to the local association by the
system, and the socket is marked as bound.
For connection-oriented sockets (for example, type SOCK_STREAM), an active
connection is initiated to the foreign host using name
(an address in the name space of the socket; for a detailed description,
please see bind
). When the socket call completes successfully, the socket is ready to
send/receive data. If the address field of the name
structure is all zeroes, connect
will return the error WSAEADDRNOTAVAIL. Any attempt to re-connect an active
connection will fail with the error code WSAEISCONN.
For a connectionless socket (for example, type SOCK_DGRAM), the operation
performed by connect
is merely to establish a default destination address which will be used on
calls. Any datagrams received from an address other than the destination
address specified will be discarded. If the address field of the name
structure is all zeroes, the socket will be "dis-connected." Then, the
default remote address will be indeterminate, so send/WSASend
calls will return the error code WSAENOTCONN. However, sendto/WSASendTo
can still be used. The default destination can be changed by simply calling connect
again, even if the socket is already "connected". Any datagrams queued for
receipt are discarded if name
is different from the previous connect
For connectionless sockets, name
can indicate any valid address, including a broadcast address. However, to
connect to a broadcast address, a socket must have setsockopt
SO_BROADCAST enabled. Otherwise, connect
will fail with the error code WSAEACCES.
When connected sockets break (that is, become closed for whatever reason),
they should be discarded and recreated. It is safest to assume that when things go
awry for any reason on a connected socket, the application must discard and
recreate the needed sockets in order to return to a stable point.
If no error occurs, connect
returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code
can be retrieved by calling WSAGetLastError
On a blocking socket, the return value indicates success or failure of the
With a nonblocking socket, the connection attempt cannot be completed
immediately. In this case, connect
will return SOCKET_ERROR, and WSAGetLastError
will return WSAEWOULDBLOCK. In this case, the application can:
- Use select to determine the completion of the connection request by checking if the
socket is writeable, or
- If your application is using WSAAsyncSelect to indicate interest in connection events, then your application will receive
an FD_CONNECT notification when the connect operation is complete, or
- If your application is using WSAEventSelect to indicate interest in connection events, then the associated event object
will be signaled when the connect operation is complete.
For a nonblocking socket, until the connection attempt completes all
subsequent calls to connect
on the same socket will fail with the error code WSAEALREADY.
If the return error code indicates the connection attempt failed (that is,
WSAECONNREFUSED, WSAENETUNREACH, WSAETIMEDOUT) the application can call connect
again for the same socket.
||A successful WSAStartup must occur before using this function.
||The network subsystem has failed.
||The specified address is already in use.
||The (blocking) call was canceled through WSACancelBlockingCall.
||A blocking Windows Sockets 1.1 call is in progress, or the service provider is
still processing a callback function.
||A nonblocking connect call is in progress on the specified socket.
Note In order to preserve backward compatibility, this error is reported as
WSAEINVAL to Windows Sockets 1.1 applications that link to either WINSOCK.DLL or
||The specified address is not available from the local machine.
||Addresses in the specified family cannot be used with this socket.
||The attempt to connect was forcefully rejected.
||The name or the namelen argument is not a valid part of the user address space, the namelen argument is too small, or the name argument contains incorrect address format for the associated address family.
||The parameter s is a listening socket, or the destination address specified is not consistent
with that of the constrained group the socket belongs to.
||The socket is already connected (connection-oriented sockets only).
||The network cannot be reached from this host at this time.
||No buffer space is available. The socket cannot be connected.
||The descriptor is not a socket.
||Attempt to connect timed out without establishing a connection.
||The socket is marked as nonblocking and the connection cannot be completed
immediately. It is possible to select the socket while it is connecting by selecting it for writing.
||Attempt to connect datagram socket to broadcast address failed because setsockopt SO_BROADCAST is not enabled.
- Software for developers
Software for Android Developers
- More information resources
Unix Manual Pages