The action performed by the connect function depends on whether it is used on connection-oriented sockets (e.g. stream sockets) or connectionless sockets (e.g. datagram sockets).
When the connect function is used on a connection-oriented socket, it uses that socket to send a connection request to a listening socket which is listening for connection requests (see the listen function for more information about listening sockets). What happens next depends on whether the socket sending the connection request has its non-blocking mode enabled or disabled (see dealing with blocking for more information about the non-blocking mode of sockets).
When the connect function is used on a connectionless socket, it does not send a connection request, instead the connect function will specify the name of destination socket or sockets for the send function, and the name of source socket or sockets for the recv function. The send function will send datagrams to the destination socket or sockets, and the recv function will silently discard any datagrams received from sockets other than the source socket or sockets.
If the connect function is used on a unbound socket (see the bind function for more information), a unique name is assigned to the socket by the system, and the socket is marked as bound.
The system include file WinSock2.inc contains the following declaration for the connect function:
function connect(s : SOCKET; name : address; namelen : integer) : integer;
external dll='ws2_32.dll';
The first argument passed to the connect function is either the socket that you want to send a connection request from (connection-oriented sockets only), or the socket for which you want to specify a name for default destination and source sockets (connectionless sockets only).
The second argument passed to the connect function is either the name of the listening socket that you want to send a connection request to (connection-oriented sockets only), or the name of the default destination and source sockets (connectionless sockets only).
The third argument passed to the connect function is the length of the name passed in the second argument.
The connect function returns a value of integer type that indicates whether the call was successful. A value of zero means that the call succeeded. A value of SOCKET_ERROR indicates that the called failed, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail.
The following example function is taken from the sample sockets programs distributed with Irie Pascal, and demonstrates how to connect a socket to a listening socket.
//PURPOSE: Connects a client data socket
//PARAMETER(s):
// 1. s - The socket to be connected
// 2. port - The TCP/IP port to connect the socket to
// 3. a - The IP address to connect the socket to
procedure ConnectDataSocket(s : socket; port : integer; a : in_addr);
var
sa : sockaddr_in;
iRet : integer;
begin (* ConnectDataSocket *)
fill(sa, 0);
sa.sin_family := AF_INET;
sa.sin_port := htonl(port) shr 16;
sa.sin_addr := a;
iRet := connect(s, addr(sa), sizeof(sa));
if iRet <> 0 then
FatalSocketError('Call to connect failed');
end; (* ConnectDataSocket *)
The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.