In this article, we will discuss some important sockets functions which are almost used in any windows application which uses the sockets. The windows API used for socket programming is called WinSock. The implementation of these functions is done using C++ and requires Microsoft Windows Software Development Kit (SDK) or the earlier Platform Software Development Kit (SDK).
Windows Sockets (WinSock), a standard network API co-developed by PC network industry leaders including Microsoft, Novell, Hewlett-Packard, and FTP Software, is an extraordinary resource for Windows network programmers. Windows Sockets 2 (Winsock) empowers software engineers to make Internet, intranet, and other system applications to transmit application information. With Winsock, developers are given access to windows networking capacities, for example, multicast and Quality of Service (QoS).
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.
SOCKET accept( SOCKET s, struct sockaddr* addr, int* addrlen );
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 occurred in sockets operation.
Function bind
Description: The bind function is used on an unconnected socket, either of type connection-oriented or connection-less before (streams), before subsequent calls to the connect or listen functions.
int bind( SOCKET s, const struct sockaddr* name, int namelen );
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).
int connect( SOCKET s, const struct sockaddr* name, int namelen );
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.
int listen( SOCKET s, int backlog );
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.
int send( SOCKET s, const char* buf, int len, int flags );
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.
SOCKET socket( int af, int type, int protocol );
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.
int closesocket( SOCKET s );
If no error function returns 0 (zero). Otherwise, it returns SOCKET_ERROR.
Function shutdown
The shutdown function disables sends or receives on a socket.
int shutdown( SOCKET s, int how );
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.
int recv( SOCKET s, char* buf, int len, int flags );
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.