Socket class design and implementation
Winsock Basic knowledge is not intended to introduce Socket or Winsock's knowledge. First introduce the Winsock API function, explain the concept of blocked / non-blocking; then introduce the use of socket.
The Winsock API Socket interface is an API of network programming (usually TCP / IP protocol, or other protocols). The earliest Socket interface is a Berkeley interface, implemented in the UNXI operating system. Winsock is also a Socket model-based API and is used in the Microsoft Windows operating system class. It has increased the Windows extension function based on the Berkeley interface function. Winscok1.1 only supports TCP / IP networks, and Winsock 2.0 adds support for more protocols. Here, discuss the API on the TCP / IP network. The Socket interface consists of three types of functions: The first class is the Berkeley Socket function included in the Winsock API. Such a function is divided into two parts. The first part is a function of network I / O, such as Accept, CloseSocket, Connect, Recv, Recvfrom, Select, Send, and Sendto another part is not involved in network I / O, functions completed on local, such as bind, getPeername, getsockname, getsocketopt, htonl, htons, inet_addr, inet_nton database functions Internet information ioctlsocket, listen, ntohl, ntohs, setsocketopt, shutdow, socket, etc. the second category is to retrieve the domain name, such as communication services and protocols, such as gethostbyaddr, gethostbyname, gethostname GetProtolbyName GetProtolbyNumber, GetServerbyName, GetServbyport. The third category is Berkekley socket routine Windows-specific extension function, such as the corresponding gethostbyname WSAAsynGetHostByName (in addition to other database functions gethostname both asynchronous version), the corresponding SELECT WSAAsynSelect, determines whether the blocking function WSAIsBlocking, to obtain the last error Windsock API Information of WsagetLastError, and so on. From another angle, these functions can be divided into two categories, one is the blocking function, one is the non-blocking function. The so-called blocking function means that the program is not allowed to call another function before completing the specified task, and the transmission of this thread message will be blocked under Windows. The so-called non-blocking function means that after the operation is started, if the result can be returned immediately, otherwise the result is returned to the error message that needs to wait, and the function is returned if the task is completed. First, the asynchronous function is a non-blocking function; secondly, the database function for obtaining far information is a blocking function (so Winsock provides its asynchronous version); in the Berkeley Socket function section