Sunday 2 August 2015

Proxy Server for Asynchronous communication using Multithreading In C


This is the third and last article in the series of socket programming in Linux using C.

Find my previous two articles below for basics:
1. http://amritapnitc.blogspot.in/2015/07/echo-server-using-tcp-and-udp-protocol.html
2. http://amritapnitc.blogspot.in/2015/07/simple-proxy-server-in-c-using-multi.html

In this article, my main objective is to modify proxy server to support asynchronous client-server communication.
Refer to the codes, presented in 2nd article. These files I have modified(specially proxyserver's code/proxy.c file)  for asynchronous communication.

You can place this proxy.c file in between any client-server communication. I tested this proxy.c file using telnet, http and FTP protocol. In case of FTP, proxy supports only client to server connection part i.e. initial connection establishment. Data exchange is not supported by this proxy since it requires additional modification to the flow of code which is not the main aim of this article.

Before jumping to the code, one interesting point to note is that the same functionality can be implemented using 'select' command of sockets.
I'll publish that code as well. As of now, lets stick to the multi-threading concepts.

How this programs works or workflow of Proxy server (proxy.c file):

  1.  Each proxy will connect to one server. Server's IP address and port address will be provided as command line arguments to proxy server.
  2. Proxy server will accept each valid communication request from client
  3. Proxy will create a separate thread for each client and thread runs a function called 'sendToServer' continuously for sending data to server received from corresponding client.
  4. A structure is maintained to pass server information to that client thread
  5. Again, one more thread is created in earlier created thread for receiving data from server
  6. This way, asynchronous communication is achieved in proxy server's code i.e: 
    1. 1st thread is created when proxy accepts a client connection. That thread will be responsible for sending data to server received from client
    2. 2nd thread is created within 1st thread to receive data from server and sent it to corresponding client.
  7. Second thread will run function called 'receiveFromServer'. This thread also maintains a structure for server information.
Now, most of the things are pretty derived from first two articles. Mainly, asynchronous way of communication is achieved using implementing two separate functions. And each of these functions will run in separate threads to maintain mutual exclusiveness. Code is simple to understand but thread management is a critical task in this approach.

How to compile:  

Please go through 2nd article containing detailed instructions with images for compilation of these programs.

Codes:
Proxy Server for asynchronous communication

Thats all folks..!! Cheers. Learn + Code + Share.

Have a nice day.. :) 

No comments:

Post a Comment