Today we will discuss some important sockets functions which are almost used in any windows application which uses the sockets. These references are from the WinSock Version 2. So be sure to try them using the Version 2 or later.

Windows Sockets Reference – WinSock2

Function accept

Description: The accept function accepts an incoming request for connection attempt on a socket.

Parameters:

s: This identifies a socket that is in a listening state with the listen function. The connection is actually made with the socket that is returned by accept.
addr: Optional pointer to a buffer that receives the address of the connecting entity, as known to the communications layer. The exact format of the addr parameter is determined by the address family that was established when the socket from the SOCKADDR structure was created.
addrlen: Its an parameter of type pointer to an integer that contains the length of addr.

If everything goes well then it returns the Socket, and if there is an error then INVALID_SOCKET value is returned. To get the specific error code you can call WSAGetLastError function to get the last error occured in sockets operation.

Function bind

Description: The bind function is used on an unconnected socket, either of type connection-oriented or connection-lessbefore (streams), before subsequent calls to the connect or listen functions.

Parameters:

s: This identifies an unbound socket.
name: Address to assign to the socket from the SOCKADDR structure.
namelen: Length of the value in the name parameter, in bytes.

If no error occurs while executing the bind function, bind function returns 0 (zero). Otherwise, it returns SOCKET_ERROR.

Function connect

Description: The connect function is used to establish a connection to a specified destination (socket).

Parameters:

s: This identifies an unbound socket.
name: Name of the socket in the SOCKADDR structure to which the connection should be established.
namelen: Length of name, in bytes

If no error occurs while executing the bind function, bind function returns 0 (zero). Otherwise, it returns SOCKET_ERROR.

Function listen

Description: Listen function is typically used in server applications where the application listens to the connection requests. The listen function places a socket in a state in which it is listening for an incoming connection.

Parameters:

s: It identifies an unconnected socket.
backlog: It defines the maximum value of the pending connections in the connections queue..

If no error occurs while executing the listen function, listen function returns 0 (zero). Otherwise, it returns SOCKET_ERROR.

Function send

Description: The send function is used to send outgoing data on a connected socket.

Parameters:

s: Connected Socket.
buf: Buffer containing the data to be transmitted to the socket.
len: Length of the data in buf, in bytes.
flags: Indicator specifying the way in which the call is made.

If no error occurs while executing the send function, send function returns the number of bytes transmitted. Otherwise, it returns SOCKET_ERROR.

Function socket

Description: The socket function causes a socket and any related resources with the socket to be allocated and bound to a specific transport-service provider.

Parameters:

af: Address family specification.
type: Type of the new socket.
protocol: Protocol to be used with the socket that is specific to the indicated address family.

If everything goes well while executing the socket function, it returns a descriptor (socket) referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Function closesocket

Description: The closesocket function closes an existing socket.

If no error function returns 0 (zero). Otherwise, it returns SOCKET_ERROR.

Function shutdown

The shutdown function disables sends or receives on a socket.

Parameters

s: Socket.
how: Flag that describes what types of operation will no longer be allowed.

Function recv

Description: The recv function is used to read incoming data on connection-oriented sockets, or connectionless sockets. When using a connection-oriented protocol, the sockets must be connected before calling recv. When using a connectionless protocol, the sockets must be bound before calling recv.

Parameters

s: Connected Socket.
buf: Buffer for the incoming data.
len: Length of buf, in bytes
flags: Flag specifying the way in which the call is made.