Contents | Prev | Next

12.10.1 The accept function

Description

The accept function allows a listening socket to establish a connection. The underlying network service provider will queue-up requests to connect to the listening socket, and this function will extract the first connection request from the queue. You can specify the maximum size of the connection request queue when you put the socket in listening mode (i.e. when you call the listen function).

Declaration

The system include file WinSock2.inc contains the following declaration for the accept function:

function accept(s : SOCKET; addr : address; var addrlen : integer) : SOCKET;
  external dll='ws2_32.dll';

Arguments

The First Argument

The first argument passed to the accept function is the listening socket that you want to accept a connection request.

The Second Argument

The second argument passed to the accept function can be used to retrieve the name of the socket whose connection request was accepted.

If you are interested in the name of the socket whose connection request was accepted, then you should use the built-in function addr to pass the operating system address of a variable into which the accept function should store the socket's name. See the bind function for more information on socket names.

If you are not interested in the name of the socket whose connection request was accepted, then you should use null for this argument.

The Third Argument

The third argument passed to the accept function is a variable of integer type, and is passed by reference. When you call the accept function, the value stored in this argument is either the size of the variable that was passed in the second argument, or null if no variable was passed in the second argument. If this argument is not null then when the accept function returns, this argument contains the actual length of the name stored in the second argument.

Return Values

The accept function returns a new socket that is connected to the socket whose connection request was accepted, if the call is successful. If the call fails then the value INVALID_SOCKET is returned, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail. NOTE: INVALID_SOCKET is declared in the system include file WinSock2.inc.

After a connection request is accepted, the listening socket continues to be available to accept more connections, and the new connected socket should be used to send data to and receive data from the socket that requested the connection.

Example

The following code fragment shows one way to accept connections.

 ConnectedSocket := accept(ListeningSocket, null, null);
 if ConnectedSocket <> SOCKET_ERROR then
  begin
   ...
   use the connection to send and receive data
   ...
  end
 else
  begin
   ...
   handle the error
   ...
  end

Reference Information

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.

Contents | Prev | Next