Wednesday, 22 February 2012

Construct your own telnet or FTP

Here i present a simple telnet construction tutorial and also how it can be used also to transfer files between server and client or a simple ftp server client implementation.With codes.

A telnet is something like a medium through which you can connect to a server and execute instructions from your system ,on the server.Here you are remotely transferring instruction which gets executed in the server as though you were directly interacting with the server.

I intend to implement the same with additional features of downloading or uploading files from and to the server.
An attempt for simplifying the process is made.The logic is this
The instruction from client to server and the response is transferred through sockets,  (Please refer to my sockets tutorial) tutorial

The Program should also resolve between whether the instruction is to be executed in the client system or the server also sockets allow data transaction in character stream form,so how to transfer files ?

For transferring files read and write commands are used which reads and writes to a data entity pointed by a file descriptor thus the file is converted into bytes and it is this bytes which gets transferred from server to client or the other way round,This allows you to transfer not only text files but also executable,audio,video,images and so on.

ok check out the server side and client side codes and lets go through them server side and the client side

Lets see the programs but before that you may want to check out the sockets tutorials mentioned earlier.

First thing you may want to observe in the server side is that signal handlers like  sigaction ,SIGCHLD are used these are to ensure that the termination of child process is successful and it may not result in zombie process ,there exist two sockets one for listening and the other is for transferring data ,all the child process handle the socket meant for transferring data.

Also for every write in server there should be a corresponding read in the client or vice verse,Here the client first transfers the data to the socket which contain the telnet commands ,observe the command change directory or cd is identified separately and instead of a system call ,a chdir() function is used this is because
in case a system call is used the subsequent commands such as say ls would reveal that the directory has actually not changed however the usage of the chdir solves the problem.


For FTP file transfer open command opens the file the name being indicated by the source and returns a file descriptor inhandle which is used by the read function to read the file and store the content into the buffer which then can be passed to the client by a simple send function.The while loop ensures that this process is repeated until the complete file is transferred .

The corresponding client side will do the opposite that is ,it will wait until the complete data is received by using the recv and write commands.Also in the client side locally system call is executed for all the instruction beginning with l ie lls is ls for client lps,ldate etc all do the same for client side.

now back to the server side,notice the

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


here the system call will store the result of the command onto a file a.txt and 2>a,txt also indicates that error message if any can also be stored onto this file.
This file is then transferred to the client where the client can then display the result.

thats about it

how to run the program ?
This intended for linux based system
1.compile your program by using
gcc server.c -o s
gcc client.c -o c

then open two terminals in one execute ./s   and in other  ./c ip or ./c 127.0.0.1  for local host   thats both client and server in your machine

then password for client is woo
once you are in you can use ls,ps,date, get filename  (file should be in server folder and you are getting it),put filename(file is in client folder and you are uploading to server folder)

importantly your server and client should be in different folders...

also once the files are uploaded or downloaded  you may want to enter the
chmod +r filename to read it
in case they are executables  use after the previous command chmod +x filename


No comments:

Post a Comment