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;

    }

Sunday 12 February 2012

FTP or telnet ,server side codes

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
   
#include <fcntl.h>

#include <sys/stat.h>



  
/* how many pending connections queue will hold */
  
    #define BACKLOG 10

     #define O_BINARY 0 // needed for the functions read and write

    void sigchld_handler(int s)

    {

        while(wait(NULL) > 0);

    }

    

    int main(int argc, char *argv[ ])

    {  /* listen on sock_fd, new connection on new_fd */
int siz=300;int po=3490; /* the port users will be connecting to */



    FILE *fp;

    char buffer[ 300 ], source [ 128 ], target [ 128 ] ;

    int inhandle, outhandle, bytes ;

    char buf[siz],rxstring[30],ch,tempc[30],tempc1[30];

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

 
/* my address information */
    struct sockaddr_in my_addr;

   
  /* connector’s address information */
    struct sockaddr_in their_addr;

    int sin_size;

    struct sigaction sa;

    int yes = 1;

    

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

    {

        perror("Socket error");

        exit(1);

    }

   

    

    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
exit(1);

  
    

  /* host byte order */

    my_addr.sin_family = AF_INET;

    /* short, network byte order */
    my_addr.sin_port = htons(po);

    /* automatically fill with my IP */

    my_addr.sin_addr.s_addr = INADDR_ANY;

    

    printf("Server  %s and port %d...\n", inet_ntoa(my_addr.sin_addr), po);

    
  /* zero the rest of the struct */
   

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

    

    if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1)
exit(1);


    

    if(listen(sockfd, BACKLOG) == -1)
exit(1);

 

   

    

      /* clean all the dead processes */

    sa.sa_handler = sigchld_handler;

    sigemptyset(&sa.sa_mask);

    sa.sa_flags = SA_RESTART;

    

    if(sigaction(SIGCHLD, &sa, NULL) == -1)
exit(1);

   // loop to accept client requests
    while(1)

    {

    sin_size = sizeof(struct sockaddr_in);

    if((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1)
continue;

  else

      

    printf("Server pinged from %s\n", inet_ntoa(their_addr.sin_addr));

      /* this is the child process */

  
    if(!fork())

    {
   /* child doesn’t need the listener */
      
       close(sockfd);
a:
while(1)
{

    numbytes = recv(new_fd, buf, siz-2, 0);


    if(numbytes > 0)
    {
//Client-The recv() is OK.
    if(buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ' )
    {
               for(k=0;k<25;k++)
        {
            tempc[k] = buf[k + 3];

        }



        chdir(tempc);
       
    }



    else if(buf[0] == 'g' && buf[1] == 'e' && buf[2] == 't' && buf[3] == ' ')
    {
   
        for(k=0;k<30;k++)
            source[k] = buf[k+4];
             inhandle = open ( source, O_RDONLY | O_BINARY ) ;
             if ( inhandle == -1 )
             {
                    puts ( "Cannot open file" ) ;
                    exit(0) ;
               }


        do
        {



bytes = read ( inhandle, buffer, 298 );
           

            i=send(new_fd, buffer, bytes, 0);
      
          }while ( bytes ==298);



    close(inhandle);   
goto a;

    }

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

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

do
        {

        bytes = recv(new_fd, buffer, 298,0);
       
                      write ( outhandle, buffer, bytes ) ;
                

        }while(bytes == 298);

strcpy(tempc1,"chmod +r ");
strcat(tempc1,rxstring);
system(rxstring);

        close(outhandle);
goto a;

}
else
{
    strcat(buf, " > a.txt 2>a.txt");
    system(buf);






        fp=fopen("a.txt","r");



    k=0;
    while ( 1 )
    {    k++;
         ch = fgetc ( fp ) ;
         if ( ch == EOF )   
         {
               buf[k-1] = ch;
           i=send(new_fd, buf, k, 0);
               break ;
         }
         else
         {
        if(k <= 298)
               buf[k-1] = ch;
        else
           i=send(new_fd, buf, k, 0);
         }


    }
           i=send(new_fd, buf, 0, 0);


    fclose(fp);
}
  }



}//Client-Received
       if(i == -1)

            perror("Server send error ");

       close(new_fd);

       exit(0);

    }

   
    


//redundant for parent process
    close(new_fd);
//printf("Server-new socket, new_fd closed successfully...\n");
  
    }

    return 0;

    }