Yes, indeed, you're gonna have to serve somebody... Bob Dylan, Gotta Serve Somebody
You are going to build a basic twitter-like service - with users "following" other users and users messaging each other. This will necessitate a server (which needs to be online for the operation to work) and various clients (i.e., the users) which converse via the server.
Implement a program, called twitServer, that accepts a single variable - its port number. This server will listen to incoming connections on the specific port, and handle client requests. The server should also maintain a log file (the file should be named twitServer.log), which includes all the commands it received (see command table below for instructions on log format). At any time the user can enter EXIT in the stdin (terminal) of the server and the server will disconnect all the clients and exit.
While we are implementing a twitter like service, the service is temporary - there is no need for the server to remember which client follows which between different sessions, nor does it need to remember a client after it disconnected (i.e., if a client A followed client B, if client B disconnects and then connects again, client A no longer follows client B).
Implement a program, called twitClient, which takes 3 program arguments - client name, server address, and port, hence usage is:
twitClient <CLIENT NAME> <SERVER ADDRESS> <SERVER PORT>
Upon execution the client will try to connect to the specified server and pass its name to the server (you decide the protocol for this). After successfully connecting the client will print "Connected successfully" to its terminal screen and wait for commands from stdin (keyboard). If a client tries to connect and the given <CLIENT NAME> is already in use, the client should fail to connect to the server - an appropriate error message should be printed to the screen, and the program should exit(1).
The client will support all the commands specified below. A command is defined to be one line, i.e. all the text typed until ENTER is pressed is considered a single command. Every command entered is sent to the server. In addition, the client will also recognize the command EXIT, which will cause the client to disconnect from the server and exit. In case the server exits first the client should behave reasonably (=not crash), but you can choose the way (detail it in the README file).
When the client receives a message (including errors) from the server it will print it immediately on the screen. Errors should be preceded by Error:.
| Function | Command | Arguments | Command description | Log format |
|---|---|---|---|---|
| Connect | When client connects to server | <CLIENT NAME>\t connected.\n | ||
| Disconnect | EXIT | When client disconnects from server | <CLIENT NAME>\t disconnected.\n | |
| Follow | FOLLOW | <SOME CLIENT NAME> | Any messages from <SOME CLIENT NAME> will be sent to the requesting client. A client may use this command even if he already follows <SOME CLIENT NAME> - there will be no effect. If <SOME CLIENT NAME> doesn't exist, the message Error: <SOME CLIENT NAME> does not exist should be sent to the requesting client. | <CLIENT NAME>\t followed <SOME CLIENT NAME>.\n |
| Unfollow | UNFOLLOW | <SOME CLIENT NAME> | No further messages from <SOME CLIENT NAME> will be sent to the requesting client. A client may use this command even if he doesn't follow <SOME CLIENT NAME> - there will be no effect. If <SOME CLIENT NAME> doesn't exist, the message Error: <SOME CLIENT NAME> does not exist should be sent to the requesting client. | <CLIENT NAME>\t unfollowed <SOME CLIENT NAME>.\n |
| Twit | TWIT | <MESSAGE> | The message must be no more than 140 characters, and it needs to be sent to all clients which are following the twitting client (in the case of extremely unpopular clients, no message will be sent). The format that needs to be sent to the following clients is:
<HOUR>:<MINUTES>::<CLIENT NAME> - <MESSAGE>
With the hour & minute being the time the message is broadcast. | <CLIENT NAME>\t twitted <MESSAGE>.\n |
| Direct message | DM | <SOME CLIENT NAME>@<MESSAGE> | The message must be no more than 140 characters, and it should be sent only to <SOME CLIENT NAME> whether he follows <CLIENT NAME> or not. The format that needs to be sent to the intended client is:
<HOUR>:<MINUTES>::<CLIENT NAME> - @<SOME CLIENT NAME>--<MESSAGE>
With the hour & minute being the time the message is broadcast. If <SOME CLIENT NAME> doesn't exist, the message Error: <SOME CLIENT NAME> does not exist should be sent to the requesting client. | <CLIENT NAME>\t direct messaged <SOME CLIENT NAME>\t <MESSAGE>.\n |
| Who | WHO | Returns the list of clients currently connected to the sever (alphabetically), e.g: <CLIENT NAME> \t <CLIENT NAME>\t ... | <CLIENT NAME>\t Who.\n |
socket(2), bind(2), listen(2), accept(2), connect(2), send(2), recv(2), close(2).
Auxiliary functions:
select(2),gethostbyname(3).
make will create a program called "twitServer" and a program called "twitClient"