TCPIP communication with Wininet classes

xiaoxiao2021-03-06  18

content:

Abstract: Winsock is the standard of TCP / IP communication between Windows applications. This is based on the API-Wininet provided by this Visual C 4.2, so that the Internet program has become fast and convenient. This paper studies how to use the Wininet class to implement TCP / IP communication. Keywords: Internet programming Winsock TCP / IP FTP HTTP GOPHER

1 Internet programming Internet programming Internet Programming Internet is a UNIX machine, and the standard for TCP / IP communication is the Berkeley Sockets protocol. With the formation and development of the Windows operating system, learn from Berkeley Sockets, create standard -Windows Sockets (Winsock) standards for TCP / IP communication between Windows applications. On this basis, some of the APIs that facilitate programmers in accordance with the client / server (Client / Server) mode. Therefore, programmers can implement TCP / IP communication according to different needs: Use Winsock; use the message API (Message API); use Internet Server API (ISAPI); using Visual C 4.2 or later Internet Releases INTERNET Class and function - Wininet. Among them, the interface of the Winsock.dll, WSOCK32, etc., which guarantees how the Socket interface is the same regardless of the mechanism of the dynamic library with the Mechanism of the Socket feature in different Windows environments (such as Windows95 or Windows NT). . Winsock not only implements TCP / IP communication, but also supports other protocols such as IPX / SPX. The MAPI service provides an interface (API) that uses the client service software installed by the user to send faxes, sound mail, email and other information. ISPAI is used to enhance, extend the HTTP server function, its developer manufacturing extensions and filters: extensions is a dynamic library that can be called from the web page, which is similar to the CGI application; Filters is a dynamic library that is started by the server for use Monitor or change the data of the server. Wininet provides client interfaces for Application layer protocols such as HTTP, FTP, GOPHER. Wininet makes the Internet program have become fast and convenient. This article will introduce and discuss Wininet and how to develop Internet communication programs using Wininet. 2 Wininet2.1 What is Wininet Wininet refers to the Internet function provided by Microsoft Win32. These functions included in the Wininet.dll dynamic library make programmers to access the Internet convenient to access the Internet, and then change Finger inquiry and WHOIS query can also be performed. VC 4.2 The above version of the MFC provides Wininet classes, which shields Winsock and TCP / IP protocols, and programmers only need to call these classes, without understanding the specific content of the protocol, can prepare client programs to access HTTP, FTP Gopher and other sites. 2.2 Wininet class and its function MFC provide 13 Wininet classes that implement a range of Internet access features. (1) CinternetSession: Create 1 or more Internet channels. (2) CinternetConnection: Connections established with the ChttpConnection, CGOPherConnection management application and the Internet server (HTTP server, FTP server, gopher server). (3) CINTERNETFILE: CHTTPFILE, CGOPHERFILE provides a method of accessing a remote server (HTTP server, Gopher server) file system. (4) CFILEFIND: CFTPFILEFIND, CGOPHERFILEFIND "Find the function of the Local and Remote Internet Site (FTP Server, Gopher Server).

(5) CGOPHERLOCATOR: Get the Gopher bitmill (Locator) from the Gopher site and is provided to cgopherfilefind to locate it. (6) CINTERNETEXCEPTION: Describes exceptions related to the Internet operation. 3 Prepare the Internet Client Program Internet Client Application Refers to get information from the Network Data Resources (such as Gopher, FTP, HTTP, etc.) from the network data resource (such as Gopher, FTP, HTTP, etc.). The programmer can call the Wininet client application directly to call the Win32 function or use the MFC Wininet class. In the process of preparing the Internet client program, the author has completed the HTTP query, FTP query, gopher query, FINGER query, the implementation of the Finger Query, WHOIS query. The main function of the query is to try to establish a connection with the server and then receive the response information directly from the server or obtain the control handle of the server-related file system. The implementation of different protocol query methods is described below. The Wininet class declaration in the header file "AFXINET.H", in the application that uses the Wininet class, there must be statement: #include 3.1 Access the WWW server to do an HTTP connection, the simplest method is to create a CinternetSession Object, with a certain effective HTTP site URL to call the function OpenURL (), which returns the CinternetFile file handle, which is the web page information located by this URL, which can read, write, search, etc. like processing a local file. Operation, obtain the necessary information. The procedure code is as follows: CinternetSession session; cinternetFile * file = null; try {file = (cinternetFile *) session.openurl (URL);} catch (cinternetException * pex) {file = null; pex-> delete ();} if (file) {// Refill file read, write, search ...,} where the try-catch statement captures the CinterNeTexception class error caused by the illegal URL connection, and the normal processing code is placed in TRY {...}, and the exception handling code is released. In catch () {...}. Access the WWW site, you can also use the ChttpConnection class. The specific method is that the HTTP server domain name and its port number (default 80) that perform the HTTP protocol port are parameters, call the CinternetSession :: gethttpConnection () function to establish a connection with a site, and use the ChttpConnection :: SendRequest function to the HTTP server Send a service request, return a file handle of the CHTTPFile type containing the response information.

3.2 Accessing the FTP Site First call the function CinternetSession :: getftpconnetic () Sets the FTP connection, the parameters you need are: FTP site domain name, user name, user password, FTP service port number (default is 21), access mode (passive Or active), where the username is empty, it indicates that the request anonymous FTP service, the user password is the user's Email address. This connection is used to operate the file system of the remote FTP server with the CFTPConnection class. If you use setCurrentDirectory (GetCurrentDirectory) Set (get) the current FTP directory of this connection, remove (create) the catalog with Rename, Remove, Putfile , GetFile and OpenFile are modified, deleted, removed, remove, and open and other operations, and finally use the Close method to close the connection with the FTP server. Therefore, the FTP client applications that provide a graphical interface can be prepared by MFC: (1) Editing the various controllers provided by MFC (such as Static Text, Edit Box, Button, List Box, List Control, Tree Control, etc.) Interface, provide a good command input and information output user interface; (2) Use MFC ClassWizard to add message mapping macro and processing functions that respond to user command operations to related classes; (3) Set up an FTP connection and a corresponding message mapping function Add program code to implement the operation of the FTP site file system, and display the result information to the user. Create a key code for FTP connection and remote file operation: CinternetSession session ("my ftp session"); cftpConnection * PConn = NULL; PCONN = session.GetftPConnection (LPSN, LPUN, LPPW, NP); // Example LPSN = "FTP .whnet.edu.cn ", lpun = lpw =" ", np = 21, // Access to this site anonymous FTP; ... pconn-> getfile (PRF, PLF); // PRF - Remove from FTP Site File name, // PLF - File name created by the local system PCONN-> getCurrentDirectory (STRCD); // Strcd is a pointer to the CString object ... PCONN-> Close (); session.close (); 3.3 Access Gopher Site First, use the CinternetSession :: getGopherConnection () function to establish a connection with a legal gopher site, parameter is the GOPHER server name, user name, user password, and gopher protocol port number (indeed value 70), return values ​​to control this connection CGOPherConnection The object's pointer. The members of the CGOpherConnection class CreateLocator () is used to create a cgopherlocator object. Its 3 parameters are in turn, find keywords, type flag, and return values ​​are locker (locators) of a file or directory of a Gopher server (LOCATOR) The application uses this positioner as an opaque marker for retrieving information from the Gopher server. Each Gopher locator has a specific property to determine the type of file that has been found or servers, and the members of the CGOPherlocator class getLocatortype () gets the property of the locator. The application generally uses the Gopher locator to find a gopher file for the member function FindFile () of the CGOPherFileFind class for the parameter.

The file name can be used as the second parameter of the function, returns a cgopherfile file handle, using this handle to read, write files, to obtain the required information; then call another member of the cgopherfilefind class FINDNEXTFILE (), continue looking for the same name file. Where cgopherfilefind objects are created to connect to parameters in this gopher. Find the gopher file, you can also use the members of the CGOPherConnection class OpenFile (), which also requires the Gopher locator to make parameters, returns the CGOPHERFILE file handle. Gopher establish connections and find relevant documents key code is as follows: CInternetSession session; CGopherConnection * con = NULL; CGopherFile * pgF = NULL; con = session.GetGopherConnection (host); // host a gopher server domain CGopherFileFind * pF = new CGopherFileFind ( Con -; cgopherlocator lc = con-> CreateLocator (NULL, NULL, GOPHERTYPEDIRECTORY); PGF = PF-> FindFile (LC, PFN); // PFN is filename string ... 3.4 Transferring Finger query Finger is a kind with gopher Query all online users of a site or an ancient Internet protocol for a specified user status. There is no Finger in the MFC class and the API function name, but the finIntet class or API function associated with the Gopher query can be implemented with the WinInTet class or API function associated with the Gopher query by specifying the protocol port. Internet information interactions have specific hosts and ports, and the domain name (or IP address) determines the host, port (port) determination process. The famous protocol adopts a standard port number. For example, when the application is created with a remote host domain name, port number 70, a gopher type (0), filename string, etc., the Gopher client sends this string to the remote host. Port 70, the Gopher server listening to the port 70 on the remote host, and respond to the information of the Gopher protocol after receiving the query string. The standard port of the Finger protocol is 79, the Finger server on the remote host, listens to this port and send the finger response to respond to the string from the client. Finger protocol: If the string is "\r\n", the response information is a list of information about all online users on the host; if the string is "user name \R\n", the response information is the user's related information . Therefore, in the Finger query, you can call the member function CinternetSession :: getGopherConnection () to run the remote host name and port 79 for the Finger server program to create a finger connection. When calling the member function of cgopherConnection CreateLocator () When the locator is created, the Finger query string sent by the client as the parameter II (lookup keyword), you can issue query information to the finger site, and the response information of the finger site is placed in cgopherfile. Brought back in the file.

Finger Finger establish a connection query and key code is as follows: CInternetSession session; CGopherConnection * con = NULL; con = session.GetGopherConnection (hostName, NULL, NULL, 79); CGopherLocator lc = con-> CreateLocator (NULL, qstr, GOPHERTYPETEXTFILE) // qstr is "\r\n" or "user name \R\" cgopherfile * file = con-> OpenFile (lc); ... // Read file 3.5 Use gopher to send WHOIS Query Wininet Class direct support. Similar to Finger, WHOIS responds to a string sent to its port, the standard port of WHOIS is 43. The WHOIS service is usually available by the DNS service system on the Internet. When you create a locator, the host name to be found is used as a lookup keyword, you can bring back the query results by the returned cgopherfile file. In the following WHOIS query paradigm, "rs.internic.net" is a domain name for hosting the Whois service, "www.microsoft.com" is the domain name of the host. CNTERNETSESSION SE; cgopherConnection * Con = NULL; Con = SE.GETGOPHERCONNECTION ("rs.internic.net", NULL, NULL, 43); cgopherlocator lc = con-> CreateLocator (Null, "www.microsoft.com", 0) CGOPHERFILE * file = con-> OpenFile (LC); ... // read file; the above method of using the Wininet class, easy access to HTTP, FTP, GOPHER, FINGER, WHOIS and other sites

转载请注明原文地址:https://www.9cbs.com/read-45979.html

New Post(0)