1. Introduction
---- In recent years, Internetwork communication has been implemented over the Internet, and common services such as WWW browser, FTP, and gopher have been implemented, this is a hot research point which will be made in the application of strict real-time requirements, such as online telephone calls and multi-media conferences, and it is necessary. In the Windows environment, the most basic method for configuring the communication process is to use Windows Sockets to implement the communication process, this software provides a large amount of messaging APIs Based on Windows Sockets, such as Winsock API, wininet API, and ISAPI, in addition, Alibaba Cloud strives to develop a faster and easier communication API and integrate it with MFC to make the communication programming easier.
---- MFC is the most important component of the VC programming environment. It provides users with a large batch of pre-defined classes and number of members, A large number of windows APIs are encapsulated. At the same time, the VC environment provides dedicated tools for work with the MFC on the image and code generation: appstudio source programming Editor, Appwizard and Class Wizard. MFC should be used to enable Windows programmers to use less time and power to develop complex communication procedures.
---- This article is based on some tests of the freetalk process of the Network Audio Frequency device when the writer has developed the real-time network audio frequency, introduce the common APIs in Windows environment and the MFC class for sealing them, the repeat describes how to use the casyncsocket and csocket classes of MFC to compile the network-based communication process order. These two classes are encapsulated with Winsock APIs, and make them more easy to use and more suitable for the MFC programming environment.
2. Communication API in Windows environment and corresponding MFC class
----1. Windows Sockets (Winsock) API
---- Windows Sockets defines the network programming interface of windows, which is based on the berkeli UNIX sockets of the berkeli branch of the University of gallifunia. Windows Sockets includes both the BSD wind grid examples and the Windows expansion part. For example, it is used to expand the number of letters driven by message-removing. Windows Sockets can be run in the Multi-network agreement, including TCP/IP, xns, decnet, IPX/SPX, etc. In the Win32 environment, Windows Sockets provides thread security. Winsock API defines the Application Programming Interface (Winsock API) for Winsock by means of the effort of micro-soft and quasi-standard grouping ), you can easily use the network protocols (such as TCP/IP) at the lower layer for network communication.
---- Two classes of casyncsocket and csocket are provided. The support of MFC enables the use of Winsock API to communicate with the route settings. MFC encapsulates the complex Winsock API into the class, which makes programming easier. The casyncsocket class has installed Winsock APIs one by one, providing more powerful and flexible methods for High-level network programmers. This category is based on the false settings of the programmers for network communication. The goal is to use Winsock in MFC, the sequencer is responsible for all tasks such as congestion, collation, and conversion between Unicode and MBCS. In order to provide programmers with a convenient interface to handle these tasks, MFC gave the csocket class, this class is inherited by the casyncsocket class, which provides Winsock API interfaces with higher layers than casyncsocket. The csocket class and csocketfile class work together with the carchive class to manage the data sent and received, which makes it easier to receive and send data. Csocket provides a blocking plug mode for the image, which is required for the same-step operation of carchive. Blocking letter count [ratio: receive (), send (), receivefrom (), sendto (), and accept ()] return to the control system right after the operation is complete, because casyncsock class is used if the low-layer control system and high efficiency are required; if necessary, you can use the csocket class. 2. Win32 Internet (wininet) API
---- Micro-soft releases an API that allows the Internet to use a procedure faster and easier than before: wininet API, it provides medium-and high-layer communication letters, which makes it easier for the client to communicate with the Internet. These functions provide separation layer between the scheduler and the Winsock drive. There are four types of wininet API letters: Use wininet letters, and use wininet text transfer agreement (FTP) number of letters, number of wininet gopher letters, and number of wininet ultra-text transfer negotiation (HTTP) letters.
---- In practice, MFC seals wininet API and ActiveX technology into the class, making the Internet programming easier, these classes include cinternetsession, cinternetconnection, cinternetfile, chttpconnection, chttpfile, cgopherfile, cftpconnection, cgopherconnection, cfilefind, cftpfilefind, delimiter, delimiter, and cinternetexception.
----3. Internet Server API (ISAPI)
---- Microsoft IIS is a WWW server tightly integrated with Windows NT Server, it is widely used as an Internet/Intranet server. IIS allows expansion and expansion. This is achieved through ISAPI, which describes the interface between ISAPI and internet server. The tools provided by ISAPI can be used to create internet server with high performance, high efficiency, full business security, and match the new IIS standard. In the same example, ISAPI is encapsulated by a typical class in MFC, including chttpfilter, chttpfiltercontext, chttpserver, chttpservercontext, related classes, and chtmlstream.
Iii. MFC encapsulation class of Winsock API
---- Some network operators must use the process sequence (for example, network-based e-calls and multi-media discussion tools). In this case, they must be difficult to implement, please try to send and receive data directly with Winsock. In this case, the designer should choose to directly connect to the Winsock API or the Winsock API encapsulated by MFC. In the newly developed application program, the advantages of using MFC are fully utilized. The first case should be the casyncsocket class and csocket class in MFC, these two classes are fully encapsulated with the Winsock API, and provide more convenience. This article describes how to use these two types of programming models, and introduce the number of members of the correlation and explanations of some concepts.
----1. casyncsocket class and csocket class
Figure below shows the succession of casyncsocket and csocket
---- The following figure shows the succession between the casyncsocket class and the csocket class. The csocket class is inherited by casyncsocket. In fact, in MFC, casyncsocket is encapsulated with the Winsock API one by one. Each casyncsocket is a Windows Socket for the image generation table, the casyncsocket class requires programmers to be familiar with network programming. Starting from the comparison, the csocket class is the generate class of casyncsocket, following its encapsulated Winsock API. A csocket represents a Windows Socket image with a higher layer than the casyncsocket image, csocket, csocketfile, and carchive are used to send and receive data. This makes it easier to use. Csocket provides a blocking plug mode for the image, because it is necessary to perform the same-step operation on carchive. In this case, it is necessary to explain the concept of a blocking plug: A socket can be located in the "blocking plug mode" or "non-blocking plug mode ", when a set of connected words is in the blocking plug mode (that is, the same step operation, its Blocking letter count will be returned to the control system right only after the operation is complete, it is called a blocking plug because the number of blocking functions connected to this set of characters cannot be done before the result is returned. If a socket is in a non-blocking plug-in mode (that is, a different step operation), it will be called using a letter to return immediately. In the casyncsocket class, getlasterror can be used as a member function to query the last error. if the error is wsaewouldblock, it indicates that there is a congestion, however, csocket will never return to wsaewouldblock, because it has managed to block the plug. Micro-softwares discuss the handling of non-blocking plug-ins by means of the generation of Network-connected events. However, in the csocket class, for the convenience of using carchive to handle many questions and simplified programming, some of its member functions are always of a blocking nature, because the carchive class needs to perform the same step. In the Win32 environment, if you want to use a sticky string, you should place it in the stand-alone worker thread, the multi-thread method is used to prevent the congestion from interfering with other threads, and avoid wasting the CPU time on the congestion. The multi-thread method enables programmers to enjoy the convenience of simplified programming with csocket, it does not affect the user's reaction to the user.
----2. casyncsocket Programming Model
---- In the process sequence of an MFC application, we should try to handle multiple network protocols without sacrificing the spirit of live, the casyncsocket class can be used for consideration. Its efficiency is higher than that of the csocket class. Casyncsocket class pins briefly describe the character programming model of the character stream type:
---- (1) construct a casyncsocket object, and use the create Letter of the object to generate a socket statement handle. The following two methods can be used:
Casyncsocket sock; sock. create (); // use the default parameter to generate a character stream or casyncsocket * psocket = new casyncsocket; int nport = 27; psocket-> Create (nport, SOCK-DGRAM); // specifies the end port number to generate a data packet
---- The first method produces a casyncsocket object on the stack, while the second method produces a casyncsocket object on the stack. The first "create as member" function is used to generate a character section stream for missing province parameters, in the second case, the number of create-as-you-go members is written into a number of input words using the specified port and address. The parameters of create include:
---- ① End port, uint type. NOTE: If it is a service provider, a public endpoint is used to connect the service provider. If it is a customer, the typical method is to connect the number of recognized parameters, so that the set of connected words can be selected by the user master;
---- ② Socket type. Sock-stream (default value) or SOCK-DGRAM;
---- ③ Socket address. For example, "ftp.gliet.edu.cn" or "202.193.64.33 ".
---- (2) if it is a customer program, use casyncsocket serial connect as a member program to connect to the server; if it is a server program, start listening with casyncsocket serial listen. Once you receive the connection request, call casyncsocket serial accept to start receiving. Note: casyncsocket uses an empty csocket operator as its parameter number, the "null" in this section refers to the number of new tokens that have not been called by the create member function.
---- (3) Call and use its casyncsocket class to generate a number of phone numbers for communication management.
---- (4) After the message is closed, the casyncsocket object will be destroyed. For example, if the casyncsocket object is generated on the stack, the image is automatically analyzed when the margin is out of the fixed pattern. If the image is produced on the stack, it is also the use of the new operator, it is necessary to use the delete operator as the pin to destroy the casyncsocket object.
----3. csocket Programming Model
---- Use the csocket object and the carchive and csocketfile class object. In the next step, only Step 1 is different for the operations of the customer and the server, and the other steps are the same.
---- (1) construct a csocket object.
---- (2) use the create member function of this pair to generate a socket statement handle. In the customer program, in addition to the non-essential data packets, create should use the default parameter number. For the server program, an end port must be specified when you call create. Note: carchive cannot start with the data packet (UDP), because the data packet is written, the usage of casyncsocket and csocket is similar.
---- (3) if the customer accesses the domain name, call casyncsocket interconnect connect to connect the domain name with the service provider. If the domain name is set by the service provider, then, call casyncsocket listener listen to enable the listener to request the connection from the customer. After receiving the connection request, call casyncsocket ACCEPT to accept the request, establish a connection. Note: The accept membership function requires a new and empty csocket parameter for the image.
---- (4) generate a csocketfile object and associate it with the csocket object.
---- (5) generate a carchive object for each data receiving and sending, and associate them with the csocketfile object. Carchive cannot start with the data report.
---- (6) Use carchive to send data to the customer and server. (7) after the completion of the call, the carchive, csocketfile, and csocket objects will be destroyed.
---- (Author's address: 97 Research Institute of Guilin electric sub-industry and university, 541004; Draft date: January 1, July 1999)
-
-