Creating data sockets

Data Sockets

In this manual, the term data sockets means sockets that you can use to send and receive data. The other kind of sockets are listening sockets, which you can use to listen for attempts by other sockets to connect. When you first create a socket it is a data socket by default. In order to create a listening socket you must take some extra steps (see creating listening sockets).

The CreateSocket Function

Description

The name of the function, in the Berkeley and Windows Sockets libraries, that creates sockets is socket, and the name of the return type of this function is SOCKET. Although the two names differ only in case, this is not a problem for these libraries because the libraries are designed for the C programming language, which is case sensitive. In C, socket and SOCKET are different identifiers, and so there is no conflict between the name of the function and the name of its return type. However, Pascal is not a case sensitive language, so socket and SOCKET are the same identifier and the name of the function would conflict with the name of the return type. In order to avoid this conflict the name of the function was changed to CreateSocket.

Declaration

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

function createsocket(af, typ, protocol : integer) : SOCKET;
  external dll='ws2_32.dll' name='socket';

Arguments

The First Argument

The first argument passed to the CreateSocket function is an expression of integer type. This argument indicates the address family the socket should belong to. Normally you would use the constant AF_INET as the value of the first argument. NOTE: AF_NET is declared in the system include file WinSock2.inc.

The Second Argument

The second argument passed to the CreateSocket function is an expression of integer type. This argument indicates the kind of socket to create and is normally one of the following constants.

NOTE: These constants are declared in the system include file WinSock2.inc.

The Third Argument

The third argument passed to the CreateSocket function is an expression of integer type. This argument indicates the protocol to use for the socket from the address family. Normally this argument is zero for all kinds of sockets except raw sockets.

Return Values

The CreateSocket function returns the created socket 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.

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.