Use C + + BUILDERW to do FTP server program

Source: Internet
Author: User
Tags ftp ftp commands socket

#include <stdlib.h>//required for atoi ()
#include ". \winsock.h "//winsock header file
#define PROG_NAME "Quick FTP Version 1"
#define HOST_NAME "NIC. Ddn.mil "//FTP Server Host
#define PASSWORD ' pass guest\r\n '//PASSWORD for FTP server host
#define WINSOCK_VERSION 0x0101//program requires WINSOCK VERSION 1.1
#define DEFAULT_PROTOCOL 0//No PROTOCOL specified, use DEFAULT
#define NO_FLAGS 0//NO special FLAGS specified
Char szcommandbuffer[100]; Buffer for FTP commands
LPSTR Lpszfunctionname; Pointer for function names
UINT Getreplycode (LPSTR lpszserverreply)
{
UINT ncode; Reply code as a number
char c; Temporary storage
Lpszfunctionname = "Getreplycode";
c = * (LPSZSERVERREPLY+3); Save the character
* (lpszserverreply+3) = ' "; Terminate the Code
Ncode = atoi (const char *) lpszserverreply); Convert code to number
* (lpszserverreply+3) = C; Restore the character
return (ncode); Return the reply code
}
UINT readftpserverreply (SOCKET hcontrolchannel)
{
Char sreceivebuffer[1024]; Data-storage buffer for FTP server reply
int ilength; Length of data received from FTP server

Lpszfunctionname = "Readftpserverreply";
if (Ilength = recv (Hcontrolchannel, (LPSTR) Sreceivebuffer,
sizeof (Sreceivebuffer), no_flags)) = = Socket_error)
{
int iwinsockerr = WSAGetLastError ();
wsprintf (Szcommandbuffer, "Error%d from" recv () function!! ",
IWINSOCKERR);
MessageBeep (Mb_iconhand);
MessageBox (NULL, Szcommandbuffer, LPSZFUNCTIONNAME,MB_OK | Mb_iconstop);
Return 999 to indicate an error has occurred
return (999);
}

Sreceivebuffer[ilength] = ' the ';
MessageBeep (Mb_iconasterisk);
MessageBox (NULL, (LPSTR) Sreceivebuffer, LPSZFUNCTIONNAME,MB_OK | Mb_iconinformation);
Extract the reply code from the "server reply" and "return as" an integer
Return (Getreplycode (Sreceivebuffer));
}

UINT Sendftpcommand (SOCKET hcontrolchannel, LPSTR szcommandbuffer)
{
Lpszfunctionname = "Sendftpcommand";
Send the FTP command
if (Send (Hcontrolchannel, (LPSTR) Szcommandbuffer,
Lstrlen (Szcommandbuffer), no_flags)) = = Socket_error)
{
int iwinsockerr = WSAGetLastError ();
wsprintf (Szcommandbuffer, "Error%d from" Send () function!! ",
IWINSOCKERR);
MessageBeep (Mb_iconhand);
MessageBox (NULL, Szcommandbuffer, LPSZFUNCTIONNAME,MB_OK | Mb_iconstop);
Return 999 to indicate an error has occurred
return (999);
}

Read the server ' s reply and return the reply code as an integer
Return (Readftpserverreply (Hcontrolchannel));
}

UINT Anonymousftplogin (SOCKET hcontrolsocket)
{
int Nreplycode; FTP Server reply code
int imsg = 0; Index Subscript for FTP commands

Lpszfunctionname = "Anonymousftplogin";
Char *logincommand[]={"USER anonymous\r\n", password,null};
Todo
{
Nreplycode = Sendftpcommand (Hcontrolsocket, (LPSTR) logincommand[imsg++]);
}
while (Logincommand[imsg] && Nreplycode < 400);
return (Nreplycode);
}
SOCKET Connectftpcontrolsocket (LPSTR lpszhost)
{
Lphostent lphostent; Internet host information structure
Sockaddr_in sockaddr; Socket Address Structure
Lpservent lpservent; Service Information Structure
Short Nprotocolport; Protocol Port
int nconnect; Socket Connection Results
SOCKET Hcontrolsocket; Control socket Handle

Lpszfunctionname = "Connectftpcontrolsocket";
if (!) ( Lphostent = gethostbyname (lpszhost)))
{
int iwinsockerr = WSAGetLastError ();
wsprintf (Szcommandbuffer, "Error #%d while resolving address for%s",
Iwinsockerr, Lpszhost);
MessageBeep (Mb_iconhand);
MessageBox (NULL, Szcommandbuffer, LPSZFUNCTIONNAME,MB_OK | Mb_iconstop);
return (Invalid_socket);
}

if ((Hcontrolsocket = socket (pf_inet, Sock_stream, ipproto_tcp))
= = Invalid_socket)
{
int iwinsockerr = WSAGetLastError ();
wsprintf (Szcommandbuffer, "Error #%d occurred while creating socket!! ",
IWINSOCKERR);
MessageBeep (Mb_iconhand);
MessageBox (NULL, Szcommandbuffer, LPSZFUNCTIONNAME,MB_OK | Mb_iconstop);
return (Invalid_socket);
}

Lpservent = Getservbyname ("ftp", Default_protocol);

if (lpservent = NULL)
Nprotocolport = htons (IPPORT_FTP);
Else
Nprotocolport = lpservent->s_port;
Define the socket address
sockaddr.sin_family = af_inet;
Sockaddr.sin_port = Nprotocolport;
SOCKADDR.SIN_ADDR = * ((lpin_addr) *lphostent->h_addr_list);

Connect the socket
if (Nconnect = Connect (Hcontrolsocket, (LPSOCKADDR) &sockaddr,
sizeof (SOCKADDR))
{
int iwinsockerr = WSAGetLastError ();
wsprintf (Szcommandbuffer,
"Error #%d occurred while connecting socket!!",
IWINSOCKERR);
MessageBeep (Mb_iconhand);
MessageBox (NULL, Szcommandbuffer, Lpszfunctionname,
MB_OK | Mb_iconstop);
return (Invalid_socket);
}

if (readftpserverreply (hcontrolsocket) >= 400)
return (Invalid_socket);
Else
return (Hcontrolsocket);
}
int PASCAL WinMain (HANDLE hinstance, HANDLE hprevinstance,
LPSTR lpszcmdparam, int ncmdshow)
{
Wsadata Wsadata; Winsock Implementation Details
SOCKET Hcontrolchannel; Socket handle for the control channel
UINT Nreplycode; FTP Reply Code
Lpszfunctionname = "WinMain";

if (WSAStartup (Winsock_version, &wsadata))
{
MessageBeep (Mb_iconhand);
MessageBox (NULL, "Could not load Windows Sockets DLL.",
Lpszfunctionname, MB_OK | Mb_iconstop);
return (NULL);
}

Hcontrolchannel = Connectftpcontrolsocket ((LPSTR) host_name);
Note this from-a DLL, here we are would return
Our control channel handle (if valid) for
Use by the other program modules.
if (Hcontrolchannel!= invalid_socket)
{
If we have a control channel, then login.
Nreplycode = Anonymousftplogin (Hcontrolchannel);

if (Nreplycode = = 230)//User logged in; We can proceed.
{
Sendftpcommand (Hcontrolchannel, "quit\r\n");
Closesocket (Hcontrolchannel);
}
}

WSACleanup ();
MessageBeep (mb_iconexclamation);
MessageBox (NULL, "The end!!", Prog_name, MB_OK | Mb_iconexclamation);
return (NULL);
}

Related Article

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.