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;

    }

No comments:

Post a Comment