Sunday 3 November 2013

A skeletal model for the client-proxy-server system

A skeletal model for the client-proxy-server system

Client

The client should do the following things:
  1. Read the file that's going to get sent
    • open(), fp.read() etc.
  2. Create a socket and establish a TCP connection to the proxy
    • socket.socket(), proxysock.connect() with SOCK_STREAM
  3. Send the file data stored into a variable in step 1 to the proxy
    • proxysock.sendall()
  4. Wait for an answer
    • proxysock.recv()
  5. Close the connection
    • proxysock.close()

Proxy

The proxy, being basically both a kind of a server and a client, should do the following things:
  1. Create a listening socket, wait for incoming connections from the client, accept and store the handle of an incoming connection
    • socket.socket(), proxyserversock.bind(), proxyserversock.listen(), proxyserversock.accept()
  2. Read the data from the incoming connection
    • clientsock.recv()
  3. Store the received data into a file
    • open(), fp.write() etc.
  4. Create a socket and establish a TCP connection to the server
    • socket.socket(), serversock.connect() again with SOCK_STREAM
  5. Send the data received from the client onwards to the server
    • serversock.sendall()
  6. Wait for an aswer
    • serversock.recv()
  7. Pass the answer on to the client
    • clientsock.sendall()
  8. Close the connections to the server and the client, but leave the listening socket on
    • serversock.close(), clientsock.close()

Server

The server should do the following things:
  1. Create a listening socket, wait for incoming connections from the proxy, accept and store the handle of an incoming connection
    • socket.socket(), serversock.bind(), serversock.listen(), serversock.accept()
  2. Read the data from the incoming connection
    • proxyclientsock.recv()
  3. Store the received data into a file
    • open(), fp.write() etc.
  4. Send an answer to the proxy
    • proxyclientsock.sendall()
  5. Close the connection, but leave the listening socket on
    • proxyclientsock.close()

Additional notes

After implementing the basic model described above, you can easily start adding more features. For example, you could add a sleep in the proxy between steps 3 and 4 and instead of directly sending the data received from the client to the server, you could read the file and send the contents - this way you can manually change the file before it gets sent to the server.
I also suggest you read Beej's Guide to Network Programming if you want to really learn the basics of network programming. The examples are in C, but the basic principles also apply in Python (and other languages as well).

No comments: