Showing posts with label ftp server. Show all posts
Showing posts with label ftp server. Show all posts

Sunday, 11 November 2012

How to make a web server

Any embedded device can be connected to the cloud and provide its own service,So after choosing you hardware such as a ARM kit or even a FPGA, The simplest method is to port an operating system on it.

Check link for porting linux on arm9
Check link for porting FreeRtos on arm7
check link for porting RT-Linux on powerpc (FPGA)


Once you have ported an OS , you got to enable ethernet facilities .

Then you can run a socket based program (check tutorial) , with this program running all you got to do is access your machine from any other PC connected to the ethernet by specifying the IP address.
That's it, by pinging the server it can execute any task as defined by your program,(Note socket just provides the means of connection,the program must also process data).

If i define port id as 80 and provide service of handling web pages or html contents , then i would have developed a HTTP server.

For a FTP server (check sourcecodes) you have to use a port id of 21.


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;

    }