请问哪里有ftp server的c源代码(注:不是c++)!在线等,急啊!
直接用socket实现的,谢谢了!不要vc++ mfc类库实现的,时间太急了,分析源码已经不可能,直接用socket实现的我看得更快更舒服些!

解决方案 »

  1.   

    linux 下的  自己修改头文件   #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <linux/stat.h>
    #include <math.h>
    #include <time.h>
    #include <signal.h>static char HostName[16];
    static int  hClient;
    static int  rcvtime = 1;
    static int  keepalive = 0;
    static struct sockaddr_in SockAddr;static int GetFtpSendBuf(char * Cmd, char * Entry, char * SendBuf,int SendBufLen)
    {
         char EndString[4]="\r\n";
         int EntryLen;
         
         EntryLen=strlen(Entry);
         
         if(EntryLen==0)
              return -1;
              
         memset(SendBuf,0,SendBufLen);
         sprintf(SendBuf,"%s ",Cmd);
         strncat(SendBuf,Entry,EntryLen);
         strncat(SendBuf,EndString,strlen(EndString));
         return strlen(SendBuf);
    }static int WriteFile(int iHandle , char * FileName)
    {
        FILE *fp;
        char RecvBuf[1024];
        unsigned bufSize = 1024;
        int RecvLen;
        
        memset(RecvBuf,0,bufSize);
        if((RecvLen=MyReceive(iHandle,RecvBuf,bufSize))>0)
        {
            fp=fopen(FileName,"wb");
            if(fp!=NULL)
            {
                setlock( fileno(fp), F_WRLCK );
              
                do {
                //fprintf(fp,"%s",RecvBuf);
                fwrite( RecvBuf, RecvLen, 1, fp );
                fflush(fp);
                memset(RecvBuf,0,bufSize);
                RecvLen=MyReceive(iHandle,RecvBuf,bufSize);
                   } while (RecvLen>0);
                   
                setlock( fileno(fp), F_UNLCK );
                fclose(fp);
               }
        }
        else 
            return -1;
          
        return 1;
    }void setlock(int fd, int type)
    {
    struct flock lock; lock.l_whence = SEEK_SET;
    lock.l_start = 0;
    lock.l_len =0; while(1)
    {
    lock.l_type = type;
    if((fcntl(fd, F_SETLK, &lock)) ==0)
    return;
    }
    }static int WriteRemoteFile( int hDataSocket, char * FileName )
    {
        register int c, d = 0;
        char buf[BUFSIZ], *bufp;
        int infile;
        
        if ( ( infile = open( FileName,O_RDWR | O_CREAT, 0666 )) != -1 ) 
        {
            while ((c = read(infile, buf, sizeof (buf))) > 0) 
            {
                for (bufp = buf; c > 0; c -= d, bufp += d)
                {
                    if ((d = write( hDataSocket, bufp, c)) <= 0 )
                    {
                        break;
                    }
                }
            }
            
            close( infile );
            close( hDataSocket );
            return 1;
        }
        else
            return -1;
    }static int FtpClose()

        close(hClient);
    }
    static int FtpMatchReceive(int hSocket, char * RecvBuf, char * MatchString,int RecvBufLen)
    {
          int RecvLen;
          int flag=0;
          int i;
          
          for(i=0;i<3;i++)
        {
            memset(RecvBuf,0,RecvBufLen);
            RecvLen=recv(hSocket,RecvBuf,RecvBufLen,0);
             if (strstr(RecvBuf,MatchString)!=NULL)
                {
                flag=RecvLen;
                break;
               }
            if(strstr(RecvBuf,"Login incorrect")!=NULL||strstr(RecvBuf,"login incorrect")!=NULL)
               {
                flag=-1;
                break;
               }
            if(strstr(RecvBuf,"Permission denied")!=NULL||strstr(RecvBuf,"permission denied")!=NULL)
               {
                flag=-2;
                break;
               }
            if(strstr(RecvBuf,"Password incorrect")!=NULL||strstr(RecvBuf,"password incorrect")!=NULL)
               {
                flag=-3;
                break;
               }
          }
        return flag;
    }static int SendFTPCommand(int  hSocket, char * SendBuf)
    {
        char RecvBuf[200];
        int ReturnCode;    if(send(hSocket,SendBuf,strlen(SendBuf),0)<0)
            return -1;    memset(RecvBuf,0,200);
        if(MyReceive(hSocket,RecvBuf,200)<=0)
            return -1;
    printf("%s\n", RecvBuf );
        if((ReturnCode=GetReplyCode(RecvBuf))<0)
        {
            printf("GetReplyCode %d\n", GetReplyCode );
            return -1;
        }    return(ReturnCode);
    }static int MyReceive(int hSocket, char * RecvBuf,int RecvBufLen)
    {
        int RecvLen=0;
        int  i;    for(i=0;i<1;i++) 
              {
             RecvLen=recv(hSocket,RecvBuf,RecvBufLen,0);
             if(RecvLen>1)
            break;
              }
        return RecvLen;
    }static int GetReplyCode(char * RecvBuf)
    {
        char cStr[4];
        int nCode;
        
        memset( cStr, 0, 4);
        if(*RecvBuf<'0'||*RecvBuf>'9'||*(RecvBuf+1)<'0'||*(RecvBuf+1)>'9'||*(RecvBuf+2)<'0'||*(RecvBuf+2)>'9')
           return -1;
        memcpy(cStr,RecvBuf,3);    return atoi(cStr);
        
    }static int  CreatListenSocket(int hControlSocket)
    {
        int  hListenSocket;
        struct sockaddr_in ListSockAddr;
        
        hListenSocket=socket(AF_INET,SOCK_STREAM,0);
        if(hListenSocket<0)
           return -1;
        
        ListSockAddr.sin_family=AF_INET;
        ListSockAddr.sin_port=htons(0);
        ListSockAddr.sin_addr.s_addr=INADDR_ANY; 
        
        if(bind(hListenSocket,(struct sockaddr *)&ListSockAddr,sizeof(ListSockAddr)))
            return -1;
        
        if(listen(hListenSocket,5))
           {
               close(hListenSocket);
               return -1;
           }    return hListenSocket;
        }
    static int RequestDataConnection(int  hControlSocket,int  hListenSocket)
    {
        struct sockaddr_in  RequestSockAddr,LocalSockAddr;
        unsigned int SourcePort;
        
        char IpBuf[100];
        char SendBuf[100];
        int SockNameLen;
        int ReturnCode;
        int i;
        
        SockNameLen=sizeof(RequestSockAddr);
        
        memset(&RequestSockAddr,0,sizeof(RequestSockAddr));
        if(getsockname(hListenSocket,(struct sockaddr * )&RequestSockAddr,&SockNameLen))
            return -1;
        
        SourcePort=htons(RequestSockAddr.sin_port);
        memset(SendBuf,0,100);
        
        if(getsockname(hControlSocket,(struct sockaddr * )&RequestSockAddr,&SockNameLen))
            return -1;
        sprintf( IpBuf, "%s", inet_ntoa( RequestSockAddr.sin_addr ) );    for ( i=0; i<strlen(IpBuf); i++ )
        {
            if ( IpBuf[i] == '.' ) IpBuf[i] = ',';
        }     sprintf(SendBuf,"PORT %s,%d,%d\r\n", IpBuf, SourcePort/256,SourcePort%256);
        printf("%s\n", SendBuf );/*    sprintf(SendBuf,"PORT %d,%d,%d,%d,%d,%d\r\n",
            SockAddr.sin_addr.S_un.S_un_b.s_b1,
            SockAddr.sin_addr.S_un.S_un_b.s_b2,
            SockAddr.sin_addr.S_un.S_un_b.s_b3,
            SockAddr.sin_addr.S_un.S_un_b.s_b4,
            SourcePort/256,SourcePort%256);
    */
        ReturnCode=SendFTPCommand(hControlSocket,SendBuf);    if(ReturnCode!=200)
          {
            printf("The Return Code of SendFTPCommand = %d\n",ReturnCode);
            close(hListenSocket);
            return -1;
          }
        return 1 ;
    }    static int  AcceptDataConnection(int  hListenSocket)
    {
        int  hDataSocket;
        struct sockaddr_in  AcceptSockAddr;
        int SockAddLen;
        
        SockAddLen=sizeof(AcceptSockAddr);
        memset(&AcceptSockAddr,0,SockAddLen);
        
        hDataSocket=accept(hListenSocket,(struct sockaddr *)&AcceptSockAddr,&SockAddLen);
        close(hListenSocket);
        
        if(hDataSocket<0)
           return -1;
        
        return hDataSocket;
     
    }/*
    int GetFileName(struct sockaddr_in  SockAddr, char * Suffix,char *FileName)
    {
        
        char CurrentDirectory[256];    strcat(CurrentDirectory,"");
        SetDataDirectory(CurrentDirectory);
        sprintf(FileName,"%s%d_%d_%d_%d.%s",
                CurrentDirectory,
                SockAddr.sin_addr.s_un.S_un_b.s_b1,
                SockAddr.sin_addr.S_un.S_un_b.s_b2,
                SockAddr.sin_addr.S_un.S_un_b.s_b3,
                SockAddr.sin_addr.S_un.S_un_b.s_b4,
                Suffix);
        return 1;
    }*/static void SetDataDirectory(char *dir)
    {
        if (chdir(dir))
        {
            return;
        }
        mkdir(dir,NULL);
    }
      

  2.   


    继续static int FtpInit(char* Host,char* Account,char* Passwd)
    {
        short       shPortNumber;
        long        lAddr;
        char     RecvBuf[1024];
        char     SendBuf[1024];
        int         RecvLen,SendLen;    shPortNumber=htons(21);
        lAddr=inet_addr(Host);
        memset(HostName,0,16);
        memcpy(HostName,Host,strlen(Host));
        hClient=socket(AF_INET,SOCK_STREAM,0);
        if (hClient<0)
          {
            printf("hClient<0\n");
             return -1;
          }
    /*    if(setsockopt(hClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&rcvtime,sizeof(int)))
        //if(setsockopt(hClient,SOL_SOCKET,SO_RCVTIMEO,(char *)NULL,sizeof(int)))
          {
            printf("setsockopt 1\n");
         close(hClient);
         return -1;
          }*/
        
        if(setsockopt(hClient,SOL_SOCKET,SO_KEEPALIVE,(char *)&keepalive,sizeof(int)))
        //if(setsockopt(hClient,SOL_SOCKET,SO_KEEPALIVE,(char *)NULL,sizeof(int)))
          {
            printf("setsockopt 2\n");
         close(hClient);
         return -1;
          }
        SockAddr.sin_family = AF_INET;
        SockAddr.sin_addr.s_addr = lAddr;
        SockAddr.sin_port = shPortNumber;
        
        if (connect(hClient,(const struct sockaddr *)&SockAddr,sizeof(SockAddr))<0)    
          {
         close(hClient);
         return -1;
          }    memset(RecvBuf,0,1024);
        if((RecvLen=FtpMatchReceive(hClient,RecvBuf, "220 ",1024))<=0)
          {
         close(hClient);
         return -1;
          }
        if(SendLen=GetFtpSendBuf("USER",Account,SendBuf,1024)<=0)
          {
         close(hClient);
         return -1 ;
          }
        if(SendFTPCommand(hClient,SendBuf)!=331)
          {
         close(hClient);
         return -1;
          }
        if(SendLen=GetFtpSendBuf("PASS",Passwd,SendBuf,1024)<=0)
          {
         close(hClient);
         return -1 ;
          }
        if(SendFTPCommand(hClient,SendBuf)!=230)
          {
         close(hClient);
         return -1;
          }
        
        SendFTPCommand(hClient,"TYPE I\r\n");
    }static int FtpGetFile(char * FileName, char * localFileName )
    {
         int     hListenSocket;
         int     hDataSocket;
         int     RetWriteFile;
         int     ReturnCode;
         char    RemoteFileName[256];
         char    RemoteFile[256];
         char    ExecCommand[256];
         char *  pcSubDir;
         
         
         memset(RemoteFileName,0,256);
         memset(RemoteFile,0,256);
         memset(ExecCommand,0,256);
         
         strcpy(RemoteFileName,FileName);
         memcpy(ExecCommand,"CWD ",4);
         
         pcSubDir=strrchr(RemoteFileName,'\\');
         if (pcSubDir !=NULL)
         {
             strncat(ExecCommand,RemoteFileName,RemoteFileName-pcSubDir);
             if(SendFTPCommand(hClient,ExecCommand)!=250)
             {
                printf("FtpGetFile err 1\n");
               close(hClient);
               return -1;
             }
             strcpy(RemoteFile,pcSubDir);
          }
          else 
          {
             strcpy(RemoteFile,RemoteFileName);
          }
     
          if((hListenSocket=CreatListenSocket(hClient))<0)
          {
                printf("FtpGetFile err 2\n");
                close(hClient);
                return -1;
          }
          if( RequestDataConnection(hClient,hListenSocket)<0 )
          {
              printf("FtpGetFile err 3\n");
              close(hClient);
              return -1;
          }
          memset( ExecCommand,0,256);
          memcpy( ExecCommand,"RETR ",5);
          strcat( ExecCommand,RemoteFile);
          printf("The FileName=%s\n",RemoteFile);
          strcat( ExecCommand,"\r\n");
                ReturnCode=SendFTPCommand(hClient,ExecCommand); 
          if(ReturnCode!=150)
          {
           close(hClient);
           return -1;
        }      if((hDataSocket=AcceptDataConnection(hListenSocket))<0)
            {
           close(hClient);
           return -1;
        }      if((RetWriteFile=WriteFile(hDataSocket,localFileName ))<0)
            {
           close(hDataSocket);
           close(hClient);
           return -1;
        }}static int FtpPutFile( char * localFileName, char * FileName)
    {
         int     hListenSocket;
         int     hDataSocket;
         int     RetWriteFile;
         int     ReturnCode;
         char    RemoteFileName[256];
         char    RemoteFile[256];
         char    ExecCommand[256];
         char *  pcSubDir;
         
         
         memset(RemoteFileName,0,256);
         memset(RemoteFile,0,256);
         memset(ExecCommand,0,256);
         
         strcpy(RemoteFileName,FileName);
         memcpy(ExecCommand,"CWD ",4);
         
         pcSubDir=strrchr(RemoteFileName,'\\');
         if (pcSubDir !=NULL)
           {
             strncat(ExecCommand,RemoteFileName,RemoteFileName-pcSubDir);
             if(SendFTPCommand(hClient,ExecCommand)!=250)
               {
               close(hClient);
               return -1;
           }
         strcpy(RemoteFile,pcSubDir);
           }
          else 
           {
                strcpy(RemoteFile,RemoteFileName);
           }
                
          if((hListenSocket=CreatListenSocket(hClient))<0)
            {
           close(hClient);
           return -1;
        }
          if(RequestDataConnection(hClient,hListenSocket)<0)
            {
           close(hClient);
           return -1;
        }
          memset( ExecCommand,0,256);
          memcpy( ExecCommand,"STOR ",5);
          strcat( ExecCommand,RemoteFile);
          printf("The FileName=%s\n",RemoteFile);
          strcat( ExecCommand,"\r\n");
          
          ReturnCode=SendFTPCommand(hClient,ExecCommand); 
          if(ReturnCode!=150)
            {
           close(hClient);
           return -1;
        }
          if((hDataSocket=AcceptDataConnection(hListenSocket))<0)
            {
           close(hClient);
           return -1;
        }
         if((RetWriteFile=WriteRemoteFile(hDataSocket,localFileName))<0)
            {
           close(hDataSocket);
           close(hClient);
           return -1;
        }
    }int ftpGet( char * ip, char * user, char * password, char * RemoteFileName, char * localFileName )
    {
        int result = -1;
        
        if ( FtpInit( ip, user, password ) != -1 )
        {
            if ( FtpGetFile( RemoteFileName, localFileName ) == -1 ) 
            {
             printf("FtpGetFile error!\n" );
            }
            else
            {
             printf("download file success\n");
             result = 0;
            }
            FtpClose();
        }
        else
            printf("FtpInit error!\n" );
            
        return result;
    }int ftpPut( char * ip, char * user, char * password, char * localFileName, char * RemoteFileName )
    {
        int result = -1;
        
        if ( FtpInit( ip, user, password ) != -1 )
        {
            if ( FtpPutFile( localFileName, RemoteFileName ) == -1 ) printf("FtpPutFile error!\n" );
            FtpClose();
            result = 0;
        }
        else
            printf("FtpInit error!\n" );
            
        return result;
    }int ftpGetOk()
    {
        return 1;
    }int ftpMoudle()
    {
        return 0;
    }
      

  3.   

    还有头文件 #ifndef __FTPMOUDLE_H
    #define __FTPMOUDLE_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <linux/stat.h>
    #include <math.h>
    #include <time.h>
    #include <signal.h>int ftpGet( char * ip, char * user, char * password, char * RemoteFileName, char * localFileName );
    int ftpPut( char * ip, char * user, char * password, char * localFileName, char * RemoteFileName );
    int ftpGetOk();
    int ftpMoudle();#endif