Use the wininet class for TCP/IP communication

Source: Internet
Author: User
Tags ftp connection ftp site gopher remote ftp server ftp client ftp access

Content:

Abstract: Winsock is the standard for TCP/IP communication between Windows applications. The API-wininet provided by Visual C ++ 4.2 or later based on this makes Internet program development fast and convenient. This article discusses how to use the wininet class to implement TCP/IP communication.
Keywords: Internet programming Winsock TCP/IP ftp http Gopher

1. Internet programming based on Windows OS
Early websites on the Internet were Unix machines, and their standard for TCP/IP communication was the Berkeley sockets protocol.
With the formation and development of the Windows operating system, drawing on the concept of Berkeley sockets, the Windows Sockets (Winsock) standard is created to implement TCP/IP communication between Windows applications.
On this basis, Some APIs are developed to facilitate programmers to program the internet in the Client/Server mode. Therefore, programmers can use different methods to achieve TCP/IP communication based on their needs: Use Winsock directly, use message API, and use Internet Service Provider API (ISAPI ); use the Internet class and function wininet provided by Visual C ++ 4.2 or later. The Winsock environment (Winsock. DLL, wsock32, and so on. It ensures that in different Windows environments (such as Windows 95 or Windows NT), regardless of the dynamic library name and the mechanism for implementing the socket function, the socket interfaces are the same. Winsock not only enables TCP/IP communication, but also supports other protocols, such as IPX/SPX. Mapi provides APIs that use the client service software installed to send fax, voice mail, email, and other information ). ISPAI is used to enhance and expand HTTP server functions. Its developers create extensions and filters: extensions are dynamic libraries that can be called by users from the web page. It is similar to CGI applications; filters is a dynamic library started by the server. It is used to monitor or change data from and into the server. Wininet provides client interfaces for HTTP, FTP, Gopher, and other application-layer protocols.
Wininet makes Internet program development fast and convenient. This article introduces wininet and how to use wininet to develop Internet communication programs.
2 wininet
2.1 What is wininet
Wininet is an Internet function provided by Microsoft Win32. these functions in the DLL dynamic library allow programmers to conveniently access the Internet using HTTP, FTP, and Gopher, and then perform Finger query and whois query with a little flexibility. MFC of VC 4.2 and later versions provides the wininet class, which shields the Winsock and TCP/IP protocols. Programmers only need to call the methods of these classes, the client program can access sites such as HTTP, FTP, and Gopher without understanding the specific content of the Protocol.
2.2 wininet class and its Functions
MFC provides 13 wininet classes, which implement a series of Internet access functions.
(1) cinternetsession: creates one or more Internet channels.
(2) cinternetconnection: Connection established with the sub-class (chttpconnection, cftpconnection, cgopherconnection) to the internet server (HTTP server, FTP server, and Gopher server.
(3) cinternetfile: cinternetfile and its subclass (chttpfile and cgopherfile) provide a method to access the file system of a remote server (HTTP server and Gopher server.
(4) cfilefind: cfilefind and its subclass (cftpfilefind and cgopherfilefind) are used to find files on local and remote Internet sites (FTP server and Gopher server.
(5) cgopherlocator: obtains the gopher bitwise mark (Locator) from the gopher site and provides it to cgopherfilefind for locating.
(6) cinternetexception: Describes exceptions related to Internet operations.
3. Use wininet to compile Internet client programs
An Internet client application is a program that obtains information from network data resources (servers) based on Internet protocols (such as Gopher, FTP, and HTTP. Programmers can directly call Win32 functions or use the MFC wininet class to write wininet client applications.
During the compilation of Internet client programs, the author used the wininet class to implement functions such as HTTP query, FTP query, Gopher query, Finger query, and whois query. The main function of the query is to try to establish a connection with the server, and then directly accept the response information from the server or obtain the control handle to the server-related file system. The following describes the implementation of different protocol query methods.
The Declaration of the wininet class should be included in the header file "afxinet. H". In the application that uses the wininet class, there must be a statement:
# Include <afxinet. h>
3.1 Access the WWW Server
The simplest way to create an HTTP connection is to create a cinternetsession object and call the function Openurl () using a valid HTTP site URL as the parameter. It returns the cinternetfile file handle, the content is the Web page information located by this URL. You can read, write, search, and obtain necessary information like a local file. The program code is as follows:
Cinternetsession session;
Cinternetfile * file = NULL;
Try {
File = (cinternetfile *) Session. Openurl (URL );}
Catch (cinternetexception * PEX ){
File = NULL;
Pex-> Delete ();}
If (File ){
// Read, write, and search files as needed ......;}
The try-catch statement captures the cinternetexception errors caused by illegal URL connections, and puts the normal processing code into try {...} , Put the exception handling code in catch (){...} .
You can also use the chttpconnection class to access the WWW site. The specific method is to call the cinternetsession: gethttpconnection () function to establish a connection with a site using the HTTP server domain name and the port number (80 by default) for executing the HTTP protocol port, and then use chttpconnection:: The sendrequest function sends a service request to the HTTP server. The returned value is a file handle of the chttpfile type containing the response information.

3.2 access the FTP site
First, call the function cinternetsession: getftpconnetion () to establish an FTP connection. The required parameters are: ftp site domain name, user name, user password, and FTP service port number (the default value is 21), access mode (passive or active). If the user name is empty, it indicates that the anonymous FTP service is requested, and the user password is the user's email address. Use the cftpconnection class to operate the file system of the remote FTP server through this connection. For example, use setcurrentdirectory (getcurrentdirectory) to set (obtain) The current FTP directory of the connection, and use removedirectory (createdirectory) delete (create) directories, rename, remove, putfile, GetFile, and openfile files, and perform operations such as renaming, deleting, moving, removing, and opening files, close the connection to the FTP server. Therefore, you can use MFC to compile FTP client applications that provide graphical interfaces: (1) use various controllers provided by MFC (such as static text, edit box, button, list box, list control, and tree control) to edit the dialog box interface, provides a good user interface for command input and information output. (2) use MFC classwizard to add a message ing macro and processing function that responds to USER command operations to the relevant class. (3) Establish an FTP connection, add the program code to the corresponding message ing function to perform operations on the FTP site file system and display the result information to the user.
Key code for establishing an FTP connection and remote file operations:
Cinternetsession SESSION ("My FTP session ");
Cftpconnection * pconn = NULL;
Pconn = session. getftpconnection (lpsn, lpun, lppw, NP );
// Example: lpsn = "ftp.whnet.edu.cn", lpun = lppw = "", NP = 21,
// Anonymous ftp access to the site;
......
Pconn-> GetFile (PRF, PLF );
// PRF -- file name retrieved from the FTP site,
// PLF -- name of the file created by the local system
Pconn-> getcurrentdirectory (strcd );
// Strcd is the pointer to the cstring object
......
Pconn-> close (); Session. Close ();
3.3 Access the gopher site
First, use the cinternetsession: getgopherconnection () function to establish a connection with a valid gopher site. The parameters are the Gopher server name, user name, user password, and Gopher protocol port number (the value is 70 ), the returned value is the pointer to the cgopherconnection object that controls the connection.
The createlocator () member function of the cgopherconnection class is used to create a cgopherlocator object. Its three parameters are directory or file name, search keyword, and type flag in sequence, the return value is the locator of a file or directory on the Gopher server. The application regards this locator as an opaque mark for retrieving information from the Gopher server. Each gopher locator has specific attributes to determine the type of the file or server that has been found. The member function getlocatortype () of the cgopherlocator class can obtain the attributes of the locator.
Applications generally use the gopher locator to call the findfile () member function of the cgopherfilefind class for the parameter to find a gopher file. The file name can be used as the 2nd parameter of the function. A cgopherfile file handle is returned, which can be used to read and write files to obtain the required information; next, you can call findnextfile (), another member function of the cgopherfilefind class, to continue searching for files with the same name. The cgopherfilefind object must be created using the gopher connection as a parameter.
You can also use the openfile () member function of the cgopherconnection class to find the gopher file. It also requires the gopher positioner as a parameter and returns the cgopherfile file handle.
The key code for establishing a gopher connection and searching related files is as follows:
Cinternetsession session; cgopherconnection * con = NULL;
Cgopherfile * PGF = NULL;
Con = session. getgopherconnection (host );
// Host is the Gopher server Domain Name
Cgopherfilefind * pF = new cgopherfilefind (CON );
Cgopherlocator lc = con-> createlocator (null, null,
Gophertypedirectory );
PGF = PF-> findfile (LC, PFn); // PFN is the file name string
......
3.4 use gopher to send finger queries
Finger is an ancient Internet protocol used to query the status of all online users or specified users on a site. Finger is not included in the names of the MFC class and API functions. However, you can use the winintet class or API function related to Gopher query by specifying the protocol port.
Internet information interaction corresponds to a specific host and port. The domain name (or IP address) determines the host and the port determines the process. The famous protocol uses a standard port number. For example, when an application uses four parameters, including remote host domain name, port number 70, Gopher type (0), and file name string, to create a gopher Locator (Locator, the gopher client sends this string to port 70 on the remote host, and the Gopher server listens to port 70 on the remote host. After receiving the query string, the client responds to the message according to the Gopher protocol. The standard port of finger protocol is 79. The Finger server on the remote host listens to this port and sends a finger response to respond to the string sent by the client. Finger Protocol: If the string is "\ r \ n", the response information is a list of all online users on the host; if the string is "User Name + \ r \ n ", the response information is related to the user.
Therefore, to perform a Finger query, you can call the member function cinternetsession: getgopherconnection () to run the Finger server program's remote host name and port 79 as parameters to establish a finger connection. When the createlocator () function of the cgopherconnection member is called to create a positioner, the Finger query string sent by the client is used as the parameter II (Search Keyword) to send the query information to the finger site, the finger site's response information is stored in the cgopherfile file.
The key code for establishing a finger connection and querying finger 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 \ n"
Cgopherfile * file = con-> openfile (LC );
... // Read the file

3.5 send whois query with gopher
The whois protocol is not directly supported by the wininet class. Similar to finger, whois responds to the string sent to its port. The standard port of whois is 43. The whois service is generally provided by the DNS service system on the Internet. When creating a locator, you can use the host name to be searched as the search keyword to bring back the query result from the returned cgopherfile file.
In the following whois query example, "rs.internic.net" is the domain name of the host that provides the WHOIS service, and "www.microsoft.com" is the domain name of the host to be queried.
Cinternetsession 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 the file;
The above method uses the wininet class to compile Internet client programs, allowing you to easily access sites such as HTTP, FTP, Gopher, finger, and whois

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.