MFC Tutorial--design and implementation of socket class

Source: Internet
Author: User
Tags execution function prototype functions socket types of functions

Winsock Basic knowledge

It is not intended to systematically introduce socket or Winsock knowledge. First, we introduce the Winsock API function, explain the concept of blocking/non-blocking, then introduce the use of socket.

WinSock API

The socket interface is an API for network programming (usually TCP/IP protocol, or other protocol). The earliest socket interface is the Berkeley interface, implemented in the Unxi operating system. Winsock is also an API based on the socket model, which is used in Microsoft Windows operating system classes. It is based on the Berkeley interface function and adds a message-driven Windows extension function. Winscok1.1 only supports TCP/IP networks, WinSock2.0 adds support for more protocols. Here, discuss the APIs on the TCP/IP network.

The socket interface includes three types of functions:

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

Accept, closesocket, connect, recv, Recvfrom, Select, Send, Sendto

The other part is functions that do not involve network I/O and are completed locally, such as

Bind, Getpeername, GetSockName, getsocketopt, htonl, htons, inet_addr, Inet_nton

Ioctlsocket, Listen, Ntohl, Ntohs, Setsocketopt, Shutdow, sockets, etc.

The second category is database functions that retrieve Internet information about domain names, communication services, and protocols, such as

GETHOSTBYADDR, gethostbyname, GetHostName, Getprotolbyname

Getprotolbynumber, Getserverbyname, Getservbyport.

The third category is Windows-specific extension functions for Berkekley socket routines, such as gethostbyname corresponding wsaasyngethostbyname (other database functions have asynchronous versions except GetHostName), Select the corresponding wsaasynselect, determine whether the blocked function wsaisblocking, get the last Windsock API error message WSAGetLastError, and so on.

From another angle, these functions can be divided into two classes, one is the blocking function, the other is a non-blocking function. A blocking function that does not allow a program to call another function before it completes a specified task, blocking the sending of this thread message under Windows. The so-called non-blocking function, refers to the operation after the start, if you can immediately get results to return the result, or return to indicate the results need to wait for the error message, do not wait for the task to complete the function to return.

First, asynchronous functions are non-blocking functions;

Second, the database function to obtain far-away information is a blocking function (therefore, Winsock provides its asynchronous version);

In the Berkeley socket function section, functions that do not involve network I/O and work on the local side are non-blocking functions;

In the Berkeley socket function section, a function of network I/O is a blocking function, which means that they can block execution, or they can block execution. These functions use a socket, which is a blocking function if the socket they are using is blocked, and if they use a socket that is non-blocking, these functions are non-blocking functions.

When you create a socket, you can specify whether it is blocked. By default, Berkerley socket functions and Winsock create "blocked" sockets. Blocking the socket becomes non-blocking by using the Select function or the Wsaasynselect function under the specified operation. The WSAAsyncSelect function prototype is as follows.

int WSAAsyncSelect(
SOCKET s,
HWND hWnd,
u_int wMsg,
long lEvent
);

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.