Research on the MFC Sealed Windows Communication API
Liu Yajun ---- Li Li ---- Jiang Hong Rui
I. Introduction
---- In recent years, Internet inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-inter-network communication is used in WWW, FTP, GOPHER. Required. The most basic way for communicating programming in the Windows environment is to apply communication between Windows Sockets to implement process, which provides a large number of Windows Sockets-based communication APIs, such as Winsock API, Wininet API, and ISAPI, and has been committed to development faster For easier communication APIs, integrate them with MFC to make communication programming more and more easily.
---- MFC is the most important component of the VC programming environment, which provides a large number of pre-defined classes and members of the employee, and blocks a large amount of Windows API. At the same time, VC environments provide special tools for work with MFC objects and code: AppStudio source program editing, AppWizard and Class Wizard. The MFC should be used, which allows the Windows program to develop complex communication applications with less time and effort.
. These two classes encapsulate the Winsock API and make them easier to use and more adapt to the MFC programming environment.
Second, the communication API and corresponding MFC classes under the WINDOWS environment
----
1. Windows Sockets (Winsock) API
---- Windows Sockets defines Windows network programming interface, which is based on the Berki Unix Sockets of the University of Commentary. Windows Sockets include both the BSD style, also added to the extended part of the Windows, such as the expansion function used for message driven. Windows Sockets can be operated on a multi-network protocol, including TCP / IP, XNS, DECNET, IPX / SPX, etc. Under Win32 environment, Windows Sockets is provided with a thread. Through the effort of micro-soft and standard group, the Winsock API is determined for Winsock API, which can use the next network protocol (such as TCP / IP) in non-standing network.
---- By providing two class CasyncSocket and CSocket, MFC support uses the Winsock API communication program. The MFC puts a complex Winsock API into the class, which makes the programming programming is easier. The CasyncSocket class enclose the Winsock API one by one, which provides a more powerful and flexible method for high-level network servants. This class is based on the hypothesis of the network communication. It is to use Winsock in the MFC to use WINSOCK, the program is responsible for the task of converting the character between Unicode and MBCS. In order to give the program, the interior of the program is automatically processed, and the MFC gives the CSocket class. This class is followed by the CasyncSocket class, which provides a Winsock API interface than the CasyncSocket higher. Csocket class and CSocketFile class are combined with the CARCHIVE class to manage the data sent and received, which makes the management data to be more convenient. Csocket pair icon provides blocking mode, which is to pay attention to Carchive. Blocking functions [such as Receive (), send (), received (), returned to control after the operation is completed, so use the CasyncSock class if low-level control and high efficiency are required; if you need to be convenient You can use the CSocket class. 2.WIN32 Internet (Wininet) API ---- Microsoft publishes the design of the Internet application than the previous, easier API: Wininet API, which provides a medium-high-level communication function, which enables access to major Internet protocols It is quite easy. These functions provide a separation layer between the programmer and Winsock drive. There are 4 types of Wininet API functions: Wininet Functions, Wininet File Transport Protocol (FTP) Functions, Wininet Gopher Functions, Wininet Supervisor Battle Options (HTTP) Functions.
---- In fact, the MFC WinInet API and the ActiveX technology encapsulated classes that allow Internet programming easier, these classes include CInternetSession, CInternetConnection, CInternetFile, CHttpConnection, CHttpFile, CGopherFile, CFtpConnection, CGopherConnection, CFileFind, CFtpFileFind, CGopherFileFind, CGopherLocator And CinternetException.
---- 3.netnet server API (ISAPI)
---- Micro-soft IIS is the only WWW server that is closely integrated with the Windows NT Server Operations, which is widely used as the Internet / Intranet server. IIS allows pruning the function, which is implemented through ISAPI, and ISAPI describes the interface between the Internet server. Tools with ISAPI, can build high-performance, high efficiency, full-time business, and conform to new IIS standard Internet service. Similarly, ISAPI is sealed by a typical class in MFC, including ChttpFilter, ChttpFilterContext, Chttpserver, ChttpserverContext, Related Classes, and Chtmlstream. Third, Winsock API's MFC package
---- Some network applications (such as network calls, multimedia conference tools) Real time should be uncommon, and you must directly use Winsock to send and receive data. At this time, the unit should select the Winsock API or by the Winsock API that is encapsulated by the MFC. In the newly developed application, it is the advantages of branching MFC, and the preferred program should be the CasyncSocket class and CSocket class in the MFC. These two classes have completely enclosed the Winsock API and provide more convenient. This article describes the programming models of the two classes, and the interpretation of the interpretation of the entrepreneurial function and some of the concepts.
---- 1.CasyncSocket class and CSocket class
Relay relationship between CasyncSocket class and CSocket classes
---- CasyncSocket class and CSocket classes are given by the drawings. The CSocket class is connected by CasyncSocket, and the things in the MFC are enclosed in the MFC, and each CasyncSocket pair is a Windows Socket, which is used to use the CasyncSocket class to ask the program to be familiar with the network. Compared with the CSocket class, the CasyncSocket sent class, which relapsed it to sealed Winsock API. A CSocket pair represents a higher-level Windows socket extraction than the CasyncSocket to send and receive the CSocketFile class and the CSocketFile class and the Carchive class. This is more convenient to use it. CSocket pair is provided with blocking mode, because it is to pay attention to the same step operation of Carchive. It is necessary to explain the concept of blocking here: A socket can be in "blocked mode" or "non-blocking mode", when a socket is in blocking mode (ie synchronous operation), its blocking function is not completed Will return control, the reason why the blocking is because the blocking function of this socket cannot be done before the operation is completed. If a socket is in a non-blocking mode (ie, the travel operation), it will be returned to the call. In the CasyncSocket class, you can check the last error with the getLastError member function. If the error is WSAEWOULDBLOCK, it is said that there is blocking, and CSocket will never return to WSAEWOULDBLOCK, because it is self-employed. The micro-soft suggestion will use a non-blocking model to generalize the processing of the application procedure in the application. However, in the CSocket class, it is a lot of questions and simplified programming using Carchive Processing, and some of its member functions are always blocked, which is the operation of the CARCHIVE class. Under Win32, if you need to make a sleeve texture, it should be placed in an independent worker thread, and the method of using multi-threads makes the blocking of the blocker, and does not show the CPU time in the block. Multi-threaded method can make the programming of the simplified programming of the CSocket, nor does it affect the response of the user interface. ---- 2. CasyncSocket class programming model
---- In an MFC application program, you must easily process multiple network protagonists, while not sacrificing the activity, you can consider using the CasyncSocket class, which is higher than the CSocket class. The CasyncSocket Class Pin The programming model of the word-of-line socket is described. It can be constructed as follows:
CasyncSocket Sock;
Sock.create ();
/ / Use the default to produce a word-of-one stream socket
or
CasyncSocket * psocket = new casyncsocket;
INT nport = 27;
Psocket-> Create (Nport, Sock-DGRAM);
/ / Specifying a port number to generate a data report set
---- The first method produces a CasyncSocket object on the stack, and the second method is produced in the pile of CasyncSocket objects. The first CREATE member function uses a default parameter to generate a word-of-one flow socket, and the second CREATE member function is generated by a normally coupled word with a designated port and address. Create's parameters are:
---- 1 port, uint type. Note: If it is the service party, the port is used to connect with a portal to the service party; if it is a guest, the typical method is to pick up the default parameters, so that the socket can select an available port;
---- 2Socket type. SOCK-Stream or Sock-DGRAM;
---- 3Socket Address. For example, "ftp.gliet.edu.cn" or "202.193.64.33".
---- (2) If the client program is used, use the CasyncSocket ∷Nect member function to the service party; Start reception. Note: CASYNCSOCKET User's function is used as a new and empty CSocket pair, this new object has not been called the CREATE member functions.
---- (3) Compass by his CasyncSocket class member function to enter the communication management.
---- (4) After the end of the communication, the pin is destroying the CasyncSocket object. If it is a CasyncSocket object that is born on the stack, it will automatically analyze when it is surrounded by a defined range; if it is born on the pile, it is used to use the new operation, and must use the delete operator to destroy the CasyncSocket object.
---- 3. CSocket class program ---- Enabling CSocket objects involving Carchive and CSocketFile class. In the steps of the knot of the word festival flow sleeve, only the third step is different from the 3rd step, and its steps are the same.
---- (1) Configure a CSocket object.
---- (2) Use this pair of CREATE members to generate a Socket handle. In the customer-equipped order, in addition to the need to be subjected to the text, CREATE should use the default parameter. For service equation, you must specify a port when the CREATE is called. Note: CARCHIVE cannot work with the Data Report (UDP) set, which is made to the data report, CasyncSocket, and CSocket's usage method.
---- (3) If it is a client plug-in, call CasyncSocket ∷ IfNect is connected to the service side; After receiving the connection request, call CASYNCSOCKET Upvice Accept request to establish a connection. Note: The Accept member function requires a new and empty CSocket pair as its parameter, interprets the same.
---- (4) Generate a CSocketFile object and associate it with the CSocket object.
---- (5) For the receiving and sending data, there is a CARCHIVE object, and the CsocketFile parallel is connected. Cut carchive is not working with data.
---- (6) Use the carchive object to transfer data in the customer with the service party. (7) After the communication is completed, the pin is destroying CARCHIVE, CSOCKETFILE and CSOCKET objects.
---- (author address: Guilin Electric Institute of Technology 97 Research, 541004; Received date: July 1999)