Write FTP client with VC ++ 5.0

zhaozj2021-02-11  220

Write FTP Clients with VC 5.0 With the rapid development of the Internet, the development and design of network software is becoming more and more important. The initial network software is mainly based on UNIX operating systems, with the popularity of the Windows personal operating system, and the traditional programming interface is extremely urgent to convert this new hardware platform. The VC version 5.0 MFC encapsulated CSocket class provides advanced Socket support to prepare a Windows platform-based C / S program to write an internet environment. This article uses the CSocket class to prepare a FTP client as an example to describe its use, uncover the secrets of network programming. Winsock provides a powerful function set to programmers in the form of a dynamic link library. By calling this function set, the application can complete its specific task. However, the disadvantage is that the program is more cumbersome. In order to solve this problem, Microsoft has gradually improved its basic class library (MFC) launched by the Visual C series. In particular, the newly released VC version 5.0, which encapsulates many classes related to network programming. Csocket is one of them. The CSocket class (parent class is CASYNCSocket) provides advanced socket support to complete the operation of the low layer function, which greatly reduces the programming difficulty. Here, Windows 95 is used as the development environment, and a FTP client is written in Visual C 5.0 to illustrate how to explore the development of network software in depth using the CSocket class. Considering the establishment of an FTP server in the C / S mode, the Windows 95 is selected from the version 4.00.950B, because this version contains the personal web server, providing HTTP and FTP services. First, create a basic framework for an SDI (single document interface) application. This step is relatively simple, in VC 5.0, MFC AppWizard is activated by creating a new item (Project), selecting the New option in the File menu, selecting Project, and enter the file name superftp, select OK. The subsequent step is VC automatic creation process, you can see the relevant information, no longer detail. Finally, the following major categories: CMAINFRAME, CSUPERFTPAPP, CSUPERFTPDOC, CSUPERFTPVIEW, CABOUTDLG. Second, establish a few new classes, as shown in the following table: Please refer to the relevant information about the FTP protocol, which is an important prerequisite for the correct development of the FTP client. The third step, the preparation of the specific program. Since the entire program is relatively long, the core code of the main part is given below and the attached release.

1.maFRM.cpp: ... cmainframe :: cmainframe () {// Initializes M_CtrlConn = null; m_dataconn = null; m_recvconn = null;} // Select menu item "Void CMAINFRAME: ONQUICKCONNECT () {IF (! Makeconn ()) MessageBox ("FTP Control Link Failed!", "Tips", MB_ICONWARNING); if (! MakeremoteDir ()) MessageBox ("FTP Data Link Failed!", "Tips", MB_ICONWARNING) } // Establish control link BOOL CMAINFRAME :: Makeconn () {... QuickConn DLG; // Enter server name, user name, password IF (DLG.Domodal () == idok) {fservername = dlg.m_servername; fusername = dlg.m_username; fpassword = dlg.m_password;} m_ctrlconn = new ctrlsocket (); // create a SOCKET If (m_ctrlconn-> Create (0, SOCK_STREAM, NULL) {delete m_ctrlconn;! m_ctrlconn = NULL; MessageBox ( "Socket () Establish failed! "," Tips ", MB_ICONWARNING); RETURN FALSE;} // Apply for Network Event Notification IF (! M_ctrlconn-> asyncselect (fd_read | fd_write | fd_accept | fd_connect | fd_close)) {MessageBox (" asyncselect () Error! "," Tips ", MB_ICNWARNING); RETURN FALSE;} beginwaitcursor (); // Split a connection request from the host specified by FServerName if {delete m_ctrlconn; m_ctrlconn = NULL; MessageBox (! remote server connection failure "," prompt ", MB_ICONWARNING); return FALSE;} (m_ctrlconn-> Connect (fservername, IPPORT_FTP)!) EndWaitCursor (); ...... return TRUE Here, after selecting the "Quick Connection" item of the menu, the execution result is as follows: If you select "Anonymous" login, the application automatically populates the user name and password word as: anonymous, guest @ unknown Bool CMAINFRAME :: MakeremoteDir () {if (m_ctrlconn =

= NULL) {("Please connect to the server!", "Tips", MB_ICONWARWING; RETRURN FALSE;} if (! MakeDataListen ()) {MessageBox ("Data Link Failed!", "Tips", MB_ICONWARNING; Return false;} ... Return True;} // Processing response from the remote server BOOL CMAINFRAME :: processftp () {.......} // Establish data connection "listening" function BOOL CMAINFRAME :: MakeDataListen () {...... m_dataconn = new listensocket (); / * Create an Socket and bundled with the local host address. As an example, the IP address "90.0.0.8" of the unit is inserted directly, and the local host address should be called by the corresponding function call in practical applications. Note: If you do not use a CSocket class with VC , you need to call the bind () function to make a bundle with the local host address after the Socket is created.

* / If (! M_dataconn-> create (0, SOCK_STREAM, "90.0.8")) {delete m_dataconn; m_dataconn = null; MessageBox ("Socket () established failed!", "Tips", MB_ICONWARNING; RETURN FALSE; } // Apply for online event notification if (! M_dataaconn-> asyncselect (fd_accept)) {delete m_dataconn; m_dataconn = null; MessageBox ("asyncselect () error!", "Prompt", MB_ICONWARNING); Return False;} ... IF (! M_DataConn-> Listen (5)) {MessageBox ("Listen () Error!", "Tips", MB_ICONWARNING); RETURN FALSE;} ...} // Accept data connection requests BOOL CMAINFRAME :: AcceptDataConn () {int num, nRet; SOCKADDR_IN RemoteDataAddr; num = sizeof (SOCKADDR_IN); if (m_recvconn == NULL) {m_recvconn = new Datasocket ();!} if (m_dataconn-> Accept (* m_recvconn, (LPSOCKADDR) & RemoteDataAddr, (int Far *) & num)) {MessageBox ("Accept () error!", "Prompt", MB_ICONWARWING); RETURN FALSE;} ... m_dataconn-> close (); // Apply for Network Event Notification IF (! M_recvconn-> asyncselect " (FD_READ | FD_WRITE | FD_CLOSE) {MessageBox ("asyncselect () error!", "Tips", MB _IConWarning);} Return true;} // data accept function, accept data from remote servers int CMAINFRAME :: RECVDATA () {... int nret = m_recvconn-> receive (...); if (nret> 0 ) {...} else {...} ...} 2.ctrlsocket.cpp ... Void Ctrlsocket :: OnReceive (int NERRORCODE) {// Processing Network Event FD_Read Csocket :: OnRecEive (NerrorCode);

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

New Post(0)