Monday 13 February 2012

client side program for FTP / telnet service

 #include <stdio.h>
#include <string.h>
 #include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include<string.h>
#include <netdb.h>
 #include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define O_BINARY 0
    int main(int argc, char *argv[])

    { int po = 3490 ; // the port client will be connecting to
 int siz = 300 ;  // max number of bytes we can get at once

     char buffer[ 300 ], target [ 128 ] ;
     int inhandle, outhandle, bytes ;


    int sockfd, numbytes,new_fd,i,j,k;

    char buf[siz],*ch,tempc[30],tmp[30],pwd[30];

    struct hostent *he;

      // connector’s address information

    struct sockaddr_in their_addr;

   // if no command line argument supplied
if(argc < 2)

    {

        fprintf(stderr, "clien: %s id\n", argv[0]);

     

        exit(1);

    }
   

  

  

   

   // get the host info,server ip
    if((he=gethostbyname(argv[1])) == NULL)

    {

        perror("IP error");
        exit(1);

    }

    else

        printf("IP%s\n", argv[1]);

   

    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

    {

        perror("no socket created");

        exit(1);

    }

    else

        printf("socket is goin strong yahoo!!\n");

   

    // host byte order

    their_addr.sin_family = AF_INET;
  // short, network byte order
  
    printf("server ip %s port id %d...\n", argv[1], po);

    their_addr.sin_port = htons(po);

    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
 
 
// zero the rest of the struct

    memset(&(their_addr.sin_zero), '\0', 8);

   

    if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)

    {

        perror("connection error ,too bad try again!!");

        exit(1);

    }

    else

        printf("Server pinged,you are good to go\n");
 printf("State thy password and ye shall pass\n");
gets(pwd);
if(strcmp(pwd,"woo")!=0)
{printf("Thou shall not pass\n");
exit(0);
}
else
{printf("Welcome");
system("date");
}
a:
while(1)
{  
    printf("$: ");

        for(k=0;k<siz;k++)
           buf[k]='\0';
  
  
    gets(buf);
    fflush(stdin);
  
if(buf[0]=='l' && buf[1]!='s')
{
for(k=0;k<30;k++)
        {
            tempc[k] = buf[k + 1];

        }
system(tempc);
goto a;
}

if(buf[0] == 'e' && buf[1] == 'x' && buf[2] == 'i' && buf[3] == 't')
{printf("Good Bye !!");

system("date");
  close(sockfd);
exit(0);
}
    else
{









    if(buf[0] == 'g' && buf[1] == 'e' && buf[2] == 't' && buf[3] == ' ')
    {
         printf ( "\nEnter target file name" ) ;
         gets ( target ) ;//target is locally used ,the file copied is stored by the target content name
  

outhandle = open ( target, O_CREAT | O_BINARY | O_WRONLY,
                 S_IWRITE ) ;




            i=send(sockfd, buf, siz-2, 0);//send file name
j=0;

printf("receiving...\n");
        do
        {
j++;
if(j%300 == 0)
printf("..");
        numbytes = recv(sockfd, buffer, 298,0);
      
                      write ( outhandle, buffer, numbytes ) ;
               

        }while(numbytes > 2);
printf("enjoy your data");
        close(outhandle);
goto a;


    }

 if(buf[0]=='p' && buf[1]=='u' && buf[2]=='t' && buf[3]== ' ' )
{for(k=0;k<30;k++)
tmp[k] = buf[k+4];

 i=send(sockfd, buf, siz-2, 0);
 inhandle = open ( tmp, O_RDONLY | O_BINARY ) ;
             if ( inhandle == -1 )
             {
                    puts ( "Cannot open file" ) ;
                    exit(0) ;
               }
j=0;
do
        {

j++;
if(j%300==0)
printf("..");

bytes = read ( inhandle, buffer, 298 );
            
            i=send(sockfd, buffer, bytes, 0);
      
          }while ( bytes ==298);
printf("\ndata transfered\n");
close(inhandle);
goto a;
}




    i=send(sockfd, buf, siz-2, 0);


    do
    {
        numbytes = recv(sockfd, buf, siz-2, 0);
        buf[298] = '\0';
        printf("%s",buf);

    }while(numbytes ==298);



}


  


  
}


    printf("Closing socket\n");

    close(sockfd);

    return 0;

    }

No comments:

Post a Comment