Establish a connection to a peer.
s
A descriptor identifying an unconnected
socket.
name
The
name of the peer to which the socket is to be connected.
namelen
Remarks
This function is used to create a connection to the specified foreign association.
The parameter s specifies an unconnected datagram or stream socket
If the socket is unbound, unique values are assigned to the local association
by the system, and the socket is marked as bound. Note that if the address
field of the name structure is all zeroes, connect() will
return the error WSAEADDRNOTAVAIL.
For stream sockets (type SOCK_STREAM), an active connection is initiated
to the foreign host using name (an address in the name space of the
socket). When the socket call completes successfully, the socket is ready
to send/receive data.
For a datagram socket (type SOCK_DGRAM), a default destination is set, which
will be used on subsequent send() and recv()
calls.
On a non-blocking socket, if the return value is SOCKET_ERROR an application
should call WSAGetLastError() If
this indicates an error code of WSAEWOULDBLOCK, then your application can
either:
- Use select() to determine the completion
of the connection request by checking if the socket is writeable, or
- If your application is using the message-based
WSAAsyncSelect() to indicate interest
in connection events, then your application will receive an FD_CONNECT message
when the connect operation is complete.
Return Value
If no error occurs, connect() returns 0. Otherwise, it returns SOCKET_ERROR,
and a specific error code may be retrieved by calling WSAGetLastError()
On a blocking socket, the return value indicates success or failure of the
connection attempt.
Error Codes
WSANOTINITIALISED
WSAENETDOWN
The Windows Sockets implementation has detected
that the network subsystem has failed.
WSAEADDRINUSE
The specified address is already in use.
WSAEINTR
WSAEINPROGRESS
A blocking
Windows Sockets call is in progress.
WSAEADDRNOTAVAIL
The specified address is not available from the
local machine.
WSAEAFNOSUPPORT
Addresses
in the specified family cannot be used with this socket.
WSAECONNREFUSED
The attempt to connect was forcefully rejected.
WSAEDESTADDREQ
A destination
address is required.
WSAEFAULT
The
namelen argument is incorrect.
WSAEINVAL
The socket is not already bound to an address.
WSAEISCONN
The socket is
already connected.
WSAEMFILE
No
more file descriptors are available.
WSAENETUNREACH
The network can't be reached from this host at
this time.
WSAENOBUFS
No
buffer space is available. The socket cannot be connected.
WSAENOTSOCK
The descriptor is not a socket.
WSAETIMEDOUT
Attempt to connect timed out without establishing
a connection
WSAEWOULDBLOCK
The
socket is marked as non-blocking and the connection cannot be completed
immediately. It is possible to select() the
socket while it is connecting by select()ing it for writing.
See Also
accept(), bind(),
getsockname(), socket(),
and WSAAsyncSelect
[Back: closesocket()]
[Next: getpeername()]