Reference 1 click the Open link
Reference 2 Click the Open link
Code
Client.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <fstream> #include <WinSock2.h> #pragma comment ( LIB, "Ws2_32.lib") using namespace std, #define Port 21//FTP, # define IP_ADDR "x.x.x.x"//host address int getportnum (char* buf); BOOL Executeftpcmd (SOCKET controlsocket, char* buf, int len, int statecode); int Getstatecode (char* buf); int main () {Wsadat A dat; SOCKET Controlsocket, Datasocket; Sockaddr_in Serveraddr;int dataport, ret, Statecode;char buf[100]={0}, sendbuf[1024]={0};//initialization, very important if (WSAStartup ( Makeword (2,2), &dat)!=0)//windows Sockets asynchronous Start {cout<< "Init falied:" <<getlasterror () < <endl;system ("pause"); return-1;} Create Socketcontrolsocket=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); if (Controlsocket==invalid_socket) {cout<< "Creating Control Socket Failed:" <<getlasterror () <<endl;system ("pause"); return-1;} Build the server access parameter structure body serveraddr.sin_family=af_inet;serveraddr.sin_addr. S_un. S_ADDR=INET_ADDR (IP_ADDR);//Address Serveraddr.sin_port=htons (port);//Ports memset (serveraddr.sin_zero,0,sizeof (Serveraddr.sin_zero));//Connection Ret=connect (Controlsocket, (struct sockaddr*) &serveraddr,sizeof (SERVERADDR)); if (ret==socket_error) {cout<< "Control SOCKET Connecting Failed: "<<getlasterror () <<endl;system (" pause "); return-1;} cout<< "Control Socket connecting is success." The <<endl;//receives the return status information recv (controlsocket,buf,100,0), cout<<buf;//220//extracts the status code based on the return information, and determines if (Getstatecode ( BUF)! =) {cout<< "Error:control Socket connecting Failed" <<endl;system ("pause"); exit (-1);} User name memset (buf,0,100), sprintf (buf, "User%s\r\n", "xxxx"), Executeftpcmd (Controlsocket, buf, 100, 331);//331// Password memset (buf,0,100), sprintf (buf, "PASS%s\r\n", "xxxx"), Executeftpcmd (Controlsocket, buf, 100, 230);//230//========= ==============================//switch to Passive mode memset (buf,0,100); sprintf (buf, "pasv\r\n"); Executeftpcmd (Controlsocket, BUF, 100, 227);//227//returns an information format of---h1,h2,h3,h4,p1,p2//where h1,h2,h3,h4 is the address of the server, P1*256+P2 is the data port Dataport=getportnum (bUF);//Client data transfer Socketdatasocket=socket (AF_INET,SOCK_STREAM,IPPROTO_TCP); serveraddr.sin_port=htons (dataPort);// Change the port value in the connection parameter Ret=connect (datasocket, (struct sockaddr*) &serveraddr,sizeof (SERVERADDR)); if (Ret==socket_error) {cout<< "Data Socket connecting Failed:" <<getlasterror () <<endl;system ("pause"); return-1;} cout<< "Data Socket connecting is success." <<endl;//change the current directory memset (buf,0,100); sprintf (buf, "CWD%s\r\n", "/ftpxxxx/web/monitor");//250executeftpcmd ( Controlsocket, buf, 100, 250);//Upload file memset (buf,0,100); sprintf (buf, "STOR%s\r\n", "1.jpg"); Executeftpcmd ( Controlsocket, buf,//125file* f=fopen ("1.jpg", "RB"), if (f==null) {cout<< "The file pointer is null!" <<endl;cout<< "Error:" <<__FILE__<< "" <<__line__<<endl;exit (-1);} while (!feof (f)) {fread (sendbuf, 1, 1024x768, f); Send (Datasocket, SENDBUF, 1024, 0);} Fclose (f);//release of resources closesocket (Datasocket); closesocket (Controlsocket); WSACleanup (); System ("pause"); return 0;} From return information "227 EnTering Passive Mode (182,18,8,37,10,25). " In//Get Data port int getportnum (char* buf) {int num1=0,num2=0;char* p=buf;int cnt=0;while (1) {if (cnt = = 4 && (*p)! = ', ') { NUM1 = 10*num1+ (*p)-' 0 ';} if (cnt = = 5) {num2 = 10*num2+ (*p)-' 0 ';} if ((*p) = = ', ') {cnt++;} P++;if ((*p) = = ') ') {break;}} cout<< "The data port number is" <<num1*256+num2<<endl;return num1*256+num2;} Execute the FTP command by controlling the socket bool Executeftpcmd (socket controlsocket, char* buf, int len, int statecode) {Send (Controlsocket, buf, Len, 0); memset (buf, 0, Len), recv (Controlsocket, buf, 0); Cout<<buf;if (Getstatecode (buf) = = Statecode) {return true;} else{cout<< "The Statecode is error!" <<endl;return false;}} Gets the status code from the return message int getstatecode (char* buf) {int num=0;char* p=buf;while (P! = NULL) {num=10*num+ (*p)-' 0 ';p ++;if (*p== ') { Break;}} return num;}
Using the C language socket to enable the Windows PC to communicate with the FTP server---socket implementation FTP client