科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网网络频道用C++ Builderw做Ftp服务器程序

用C++ Builderw做Ftp服务器程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

用C++ Builderw做Ftp服务器程序

作者:巧巧读书 来源:巧巧读书 2008年6月23日

关键字: 网吧 网吧组网 网吧服务器架设

  • 评论
  • 分享微博
  • 分享邮件

  #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) = '\0'; // 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 the 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] = '\0';

  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 the 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};

  do

  {

  nReplyCode = SendFTPCommand(hControlSocket,(LPSTR)LoginCommand[iMsg++]);

  }

  while(LoginCommand[iMsg] &&nReplyCode <400);

  return(nReplyCode);

  }

  SOCKET ConnectFTPControlSocket(LPSTR lpszHost)

  {

  LPHOSTENT lpHostEnt; // Internethost 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!! ",

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章