SOCKET class design and implementation

Source: Internet
Author: User
Tags types of functions

 

Basic WinSock knowledge

We do not plan to introduce it systematically here.Knowledge about socket or WinSock. First, we will introduce the WinSock API function, introduce the concept of blocking/non-blocking, and then introduce the use of socket.

WinSock API

A Socket interface is an API for network programming (usually the TCP/IP protocol or other protocols. The earliest Socket interface is the Berkeley interface, which is implemented in the Unxi operating system. WinSock is also an API Based on the Socket model, which is used in the Microsoft Windows operating system class. Based on the Berkeley interface functions, it also adds Windows extension functions based on the message-driven mechanism. Winscok1.1 only supports TCP/IP networks, and WinSock2.0 supports more protocols. Here, we will discuss the APIs on TCP/IP networks.

The Socket interface includes three types of functions:

The first type isThe Berkeley socket function contained in the WinSock API. This type of function is divided into two parts. The first part is a function used for network I/O, such

Accept, Closesocket, connect, recv, recvfrom, Select, Send, Sendto

The other part is that the network is not involved.I/O, functions completed locally, such

Bind, getpeername, getsockname, getsocketopt, htonl, htons, inet_addr, inet_nton

Ioctlsocket, listen, ntohl, ntohs, setsocketopt, shutdow, socket, etc.

The second type is to retrieve domain names, communication services, protocols, etc.Database functions of Internet information, such

Gethostbyaddr, gethostbyname, gethostname, and getprotolbyname

Getprotolbynumber, getserverbyname, and getservbyport.

Category 3:Dedicated Windows extended functions of Berkekley socket routines, such as WSAAsynGetHostByName corresponding to gethostbyname (other database functions have asynchronous versions except gethostname), and WSAAsynSelect corresponding to select to determine whether the function is blocked, obtain the WSAGetLastError of the last Windsock API error, and so on.

From another perspective, these functions can be divided into two types: blocking functions and non-blocking functions. The so-called blocking function means that the program is not allowed to call another function before it completes the specified task.In Windows, messages in this thread are blocked. A non-blocking function is a function that returns a result immediately after the operation is started. Otherwise, an error message indicating that the result needs to be waited is returned without waiting for the task to complete.

First, asynchronous functions are non-blocking functions;

Secondly, the database function for obtaining remote information is a blocking function (therefore,WinSock provides its asynchronous version );

InIn the Berkeley socket function section, functions that do not involve network I/O and local operations are non-blocking functions;

InIn the Berkeley socket function section, network I/O functions can be blocked, that is, they can be blocked or not blocked. These functions use a socket. If the socket they use is blocked, these functions are blocking functions. If the socket they use is not blocked, these functions are non-blocking functions.

CreateYou can specify whether the socket is blocked. By default, both Berkerley's Socket function and WinSock create blocked sockets. Blocking socket is not blocked by using the select function or the WSAAsynSelect function in the specified operation. The following is a prototype of the WSAAsyncSelect function.

Int WSAAsyncSelect (

SOCKET s,

HWND hWnd,

U_int wMsg,

Long lEvent

);

Parameters1 specifies the socket handle to be operated; 2 specifies a window handle; 3 specifies a message; 4 specifies a network event, which can be a combination of multiple events, for example:

FD_READPrepare to read

FD_WRITEPrepare to write

FD_OOBOut-of-band data arrival

FD_ACCEPTReceive connection

FD_CONNECTConnect

FD_CLOSEDisable socket.

UseOR action to combine these event values, such as FD_READ | FD_WRITE

The WSAAsyncSelect function monitors network events specified by lEvent for socket s. If an event occurs, wMsg is sent to the window hWnd.

Assume thatSocket s specifies the FD_READ event monitoring, and the FD_READ event becomes non-blocking. When the read function is called, no matter whether or not the read data is immediately returned. If an error message is returned, the message wMsg is sent to the window hWnd after the waiting data arrives, the application processes the message and reads network data.

When an asynchronous function is called, a similar process is used to obtain the result data. ToThe usage of the asynchronous version of gethostbyname is described as an example. The function is prototype as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.