nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Need Help To do FTP implimentation

This is a discussion on Need Help To do FTP implimentation within the Coding in General forums, part of the Development/Scripting category; Hii I am a igner with FTP > Now I am in need to impliment FTP; The Question Is to ...

Register free or login to your existing account and remove all advertisements.


Go Back   nixCraft Linux Forum > Development/Scripting > Coding in General

Linux answers from nixCraft.


Coding in General Discussion on PHP/Perl/Python/Ruby/GNU C or C++. MySQL, PgSQL and (X)HTML or any other programming languages you want.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-12-2009, 11:46 AM
Junior Member
User
 
Join Date: Feb 2009
OS: Open Suse
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
rthcupid is on a distinguished road
Question Need Help To do FTP implimentation

Hii I am a igner with FTP > Now I am in need to impliment FTP;
The Question Is to Transfer a file (size More Than 600byts) from a client to server and get responds from the server. vice versa. I code a program Than send file but t received only a part of file in the server side . It is need to divide the file into packets . please help me to do this .. My code is following ...


Code:
/*
************************************************************************************
              FTP SERVER
************************************************************************************
*/
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(){
    int sd,fd;
    int bufsize=1024;        /* a 1K buffer */
    char *buffer=(char *)malloc(bufsize);
    //creating a TCP socket
        sd=socket(AF_INET,SOCK_STREAM,0);
    int csd,len,n;
    if(sd==-1)
    {
           printf("\n******************************************************************************");
       printf("Error in creating sockets");
           printf("\n******************************************************************************");
           exit(1);
    }
    struct sockaddr_in sadd;
    //specify address domain
    sadd.sin_family=AF_INET;
    //set IP address

    sadd.sin_addr.s_addr=htonl(INADDR_ANY);
    //specify the port used for connection
    sadd.sin_port=htons(8000);
    //associate name with socket descriptor
    int c=bind(sd,(struct sockaddr*)&sadd,sizeof(sadd));
    if(c==-1)
    {
          printf("\n***********************************************************");
      printf("Error in Binding\n");
          printf("\n***********************************************************");
          exit(1);
    }
    //publishing the socket
    int l=listen(sd,5);
    if(l==-1){
          printf("\n***********************************************************");  
      printf("Error in listening");
          printf("\n***********************************************************\n");
    }
    else{
      printf("\n*******************************************************\n\n");
      printf("Listening to Clients .....\n");
      printf("\n*******************************************************\n\n");
    }
    len = sizeof(struct sockaddr_in);
        while(1){
                 csd=accept(sd,(struct sockaddr*)&sadd,&len);
             if(csd<0){
                     printf("Error in connecting to client %d",csd);
                   return(0);
             }
                printf("\n**************************************************************");
                printf("\nConnected to client");
                
                send(csd,"\nEnter a file name         :",20,0);
                recv(csd,buffer,bufsize,0);
        printf("\n*******************************************************\n\n");
                printf("\n\nReceived filename from client : n");
        printf("\nThe Name Of the Received  File is == >  %s \n",buffer);
                fd=open(buffer,0);
                if(fd<0)
                {
                  printf("Error in Opening File\n");
                  exit(1);
                }
                n=read(fd,buffer,50);
                if(n<0){
                     printf("Error in reading\n");
                     exit(1);
                }
                printf("\n**************************************************************");
                printf("\nSending file contents to Client ............\n");
                printf("\n**************************************************************");
                send(csd,buffer,bufsize,0);
                close(fd);            
        close(csd);
    }
    return(0);
}
    
    




/*
************************************************************************************
             FTP CLIENT
************************************************************************************
*/
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<netdb.h>
int main(){
    int sd,fd,n;
    int bufsize=1024;        /* a 1K buffer */
    char *buffer=(char *)malloc(bufsize);
        char ch[20];
    struct hostent *he;
    //creating a TCP socket
    sd=socket(AF_INET,SOCK_STREAM,0);
    if(sd==-1)
    {
            printf("\n*******************************************************");
        fd=creat("newfile",1);
        printf("Error in creating sockets");
                printf("\n*******************************************************");
    }
    else{
                printf("\n*******************************************************\n\n");
            printf("Socket created\n");
        printf("\n*******************************************************\n\n");
    }
    if ((he = gethostbyname("localhost")) == NULL) {
                printf("\n*******************************************************\n\n");
            puts("error resolving hostname..");
        printf("\n*******************************************************\n\n");
            return(0);
      }
           struct sockaddr_in sadd;
    //specify address domain
    memcpy(&sadd.sin_addr, he->h_addr_list[0], he->h_length);
    sadd.sin_family=AF_INET;
    //specify the port used for connection
    sadd.sin_port=htons(8000);
    
    if(connect(sd,(struct sockaddr *)&sadd,sizeof(sadd)))

    {
        printf("\n*******************************************************\n\n");
            printf("Error in connecting");
        printf("\n*******************************************************\n\n");
             exit(1);      
    }
    printf("\n*******************************************************\n\n");
    printf("Connected to Server .. Redy for Sending Files \n");
        recv(sd,ch,20,0);
        printf("%s\n",ch);
        gets(buffer);
        printf("\n*************************************************************");
        fd=creat("Myfile.doc",1);
        printf("\n\n Sending filename to Server ..........");
    send(sd,buffer,bufsize,0);
    recv(sd,buffer,bufsize,0);
    printf("\n*******************************************************\n\n");
    printf("\nReceived data from server........");
        //printf("\n*************************************************************");
        fd=creat("Myfile.doc",1);
        if(fd<0)
        {
                printf("\n*****************************************************");
            printf("Error in creating file");
                printf("\n*****************************************************");
                 exit(1);
        }
    
        n=write(fd,buffer,100);
    printf("\n\n\n**************** The Recieved File Is *****************\n\n");
        puts(buffer);
    printf("\n\n\n*******************************************************\n\n");
        if(n<0)
        {
                printf("Error in writting to file\n");
                exit(1);
        }
        close(fd);
        return(0);
}
Reply With Quote
  #2 (permalink)  
Old 02-19-2009, 10:26 AM
Junior Member
User
 
Join Date: Feb 2009
OS: Open Suse
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
rthcupid is on a distinguished road
Default need help

Aany body know this kindly reply.........................
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT +5.5. The time now is 07:02 PM.


Powered by vBulletin® Version 3.8.4 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2009 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38