Write FTP client with VC

xiaoxiao2021-03-06  69

Ftp client with the preparation of VC: Panda reprint Date: March 28, 2002 editor: Spring program article text With the rapid development of Internet, software development and design of the network becomes 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. The MFC 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 to write an FTP client to explain how to use the CSocket class to develop network software in depth. First, create a basic framework for an SDI (single document interface) application. This step is relatively simple. In VC , MFC AppWizard is activated by creating a new project (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. The second step, the preparation of the specific procedure. 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!", "Prompt", MB_ICNWARNING); return FALSE;} BeginWaitCursor (); // send a connection to the host specified by fservername Request if (! M_ctrlConn-> Connect (fservername, ipport_ftp) {delete m_ctrlconn; m_ctrlConn = NULL; MessageBox (remote server connection failed! "," Tips ", MB_ICONWARNING; RETURN FALSE

} Endwaitcursor (); ... Return True;} If the "anonymous" login is selected, the application automatically populates the user name and password word to: 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!", "Tip", MB_ICONWARNING; RETURN False;} ...... Return True;} // Processing the response from the remote server BOOL CMAINFRAME :: processftp () {.......} // Establish data connection "listening" function BOOL CMAINFRAME :: MakeDataListen () {... m_dataconn = New listensocket (); / * Create a 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_ICONWARW Ion;} ... m_dataconn-> close (); // Apply for network event notification if (! M_recvconn-> asyncselect (fd_read | fd_write | fd_close) {MessageBox ("asyncselect () error!", "Prompt" , MB_ICONWARNING; RETURN FALSE;} Return true;} // data accept function, accept data from remote servers int CMAINFRAME :: RECVDATA () {... int nret = m_recvconn->

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

New Post(0)