Lab1 - simple client/server application
From Inforail
Contents
Keywords
client, server, TCP, IP, BSD sockets
Objective
Familiarize yourself with the BSD sockets API by writing a simple server and a client.
Generic requirements
- The program must rely on the BSD sockets API, not some other library which is an abstraction on top of BSD sockets.
- TCP must be used at the transport layer.
- IP must be used at the network layer.
Server requirements
- The server must print debug information about each incoming connection, indicating the remote address [and other details you think that are relevant].
- The server must implement 5 special commands that can be received from the client, to which the server will react in a special way:
- %Hastalavista - if the server receives this command, it will terminate the connection and shut itself down;
- %Time - the server will react by sending the client the current time of the system on which the server is running
- The other 3 commands have to be chosen by you
- If the server receives an unknown command - it must respond with "Can you elaborate on that?"
- If the server receives an unknown command that ends with a '?' - it must respond with "42"
Bonus features
- The server must be able to handle multiple client connections at the same time.
- The server supports an additional special command - %Picsoritdidnthappen. Upon receiving it, the server will send the following image to the client
Client requirements
- The client must take its input commands from the keyboard
- The command will be sent to the server by pressing the Enter key
- If you implemented the file transfer feature, the client must receive the image and save it to a file in the current directory; i.e. your client does not have to display the image itself.
- The client will terminate the connection with the server if you try to send the %Close command
Grading policy
Assuming that everything works right,
- 8 - for implementing all the basic features;
- 9 - for implementing the basic features + one of the bonus features;
- 10 - for the basics and both bonus features.
Remark on supporting multiple client connections, there are 2 ways to get that bonus:
- Describe two different ways in which the feature can be implemented (comparing each approach and discussing the pros and cons of each), or
- Implement one of the two ways
That's right, the bonus can be yours if you think about it, without actually making it work.
Notes
- The program does not have to be one with a GUI, a command line application will do.
References
- http://en.wikipedia.org/wiki/Bsd_sockets
- http://docs.python.org/howto/sockets.html
- http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html
- Through The Ether And Back Again: What Happens To A Packet When You Send It
Examples
#Here is a simple example of a server written in Python.
from socket import *
#note that you could have written import socket
#then you'd have to write "socket." as a prefix to access stuff in this module
response="I am here to serve. I want to talk about it"
#initialize the socket with default settings
listeningSocket = socket()
#bind the socket to an interface
listeningSocket.bind( ("127.0.0.1", 1045) )
print "bind = OK"
#make it listen
listeningSocket.listen(5)
print listeningSocket #print the handle to the socket, so you can see exists
#accept an incoming connection
newAllocatedSocket, address = listeningSocket.accept()
print address, newAllocatedSocket #see some data about the connected client
newAllocatedSocket.send(response) #send a message to the client
data = newAllocatedSocket.recv(10) #read 10 bytes
print data
#newAllocatedSocket.send(gethostname( ))
#close everything and get outta here
newAllocatedSocket.close()
listeningSocket.close()