Introduction
Server:
:socket,
:
Client:
:,,
, ()
:(private message), ()
:
4Len,
Len
tcp,
struct socket_info
{
int fd; socket
struct sockaddr_in addr;
};
struct user_data
{
char Name[30];
char Psd[30];
struct socket_info Con;
int State; -1 0 1
int Block; 0 10
struct timeval Time; block=1,
struct timeval LogOffTime; state-1,
int BlackList[100]; i0i 1
int Msg_count;
char Msg[100][1000];
char Privte_addr[40]; “ip ”,private
};
:
int state[100]; i=0 =1
struct socket_info P_List[100];
char name_list[100][30];
state[i]=1P_List[i]name_list[i],private
Client:
1、M:M(,)
2、P:socket,
3、IP :private,ip,,
1):
,“ ”
:
E1:
E2:
E3:,,
E4:,,
E5:
E6:
OK:
2)message
M
()(M),(M)
,,,
3)broadcast
B
message,,,,
4)whoelse
W
,
5)whoelsesince
S
S,
6)block
L
,,,
7)unblock
8)startprivate
P
,,ip,,,
ip ,B,B:
P
B
9)private
,,
M
10)stopprivate
,,
Requirement
1
COMP3331/9331 Computer Networks and Applications
Assignment for Session 1, 2017
Version 2.0
Due: 11:59pm Friday, 28 April 2017 (Week 8)
Mandatory Demo: Your regular lab slot (Week 9)
1. Change Log
Version 1.0 released on 20 th March 2017.
Version 2.0 released on 10 th April 2017.
Added a command to close the p2p messaging session. This only applies for the extended version.
2. Goal and learning objectives
Instant messaging applications such as WhatsApp, WeChat, Facebook Messenger, etc. are widely
used with millions of subscribers participating in them globally. In this assignment, you will have
the opportunity to implement your own version of an instant messaging application. In addition to
basic messaging functionality, you will also implement many additional services that are available
in many of the aforementioned applications. Your application is based on a client server model
consisting of one server and multiple messaging clients. The clients communicate with the server
using TPC. The server is mainly used to authenticate the clients and direct the messages (online or
offline) between clients. Besides, the server also has to support certain additional functions
(presence notification, blacklisting, timeout, etc.). The extended version asks you to implement
peer-to-peer messaging that bypasses the server.
2.1 Learning Objectives
On completing this assignment, you will gain sufficient expertise in the following skills:
1. Detailed understanding of how instant messaging services work.
2. Expertise in socket programming.
3. Insights into designing an application layer protocol.
3. Assignment Specification
There are two versions of this assignment, a standard version (with a total of 13 marks) and an
extended version (with a total of 15 marks of which 2 marks are bonus marks). The
Updates to the assignment, including any corrections and clarifications, will be posted on the
subject website. Please make sure that you check the subject website regularly for updates.
2
specifications for the extended version can be found in Section 5 of the specification. Note that the
bonus marks may not be proportional to the amount of extra work that you will have to do. They are
there to encourage you to go beyond the standard assignment. The bonus marks can be used to
make up for lost marks in the lab exercises and the second programming assignment but NOT for
any of the exams (mid-session and final).
The assignment includes 2 major modules, the server program and the client program. The server
program will be run first followed by multiple instances of the client program (Each instance
supports one client). They will be run from the terminals on the same and/or different hosts.
For the standard version, all messages between clients MUST be sent via the server. The extended
version asks you implement additional functionality whereby two users can exchange messages
with each other directly (i.e. bypassing the server) in a peer-to-peer fashion.
4.1. Server
The server has the following responsibilities -
User Authentication - When a new client requests for a connection, the server should prompt the
user to input the username and password and authenticate the user. The valid username and
password combinations will be stored in a file called credentials.txt which will be in the same
directory as the server program. An example credential.txt file is provided on the assignment page.
Username and passwords are case-sensitive. We may use a different file for testing so DO NOT
hardcode this information in your program. You may assume that each user name and password
will be on a separate line and that there will be one white space between the two. If the credentials
are correct, the client is considered to be logged in (i.e. online) and a welcome message is
displayed. When all messaging is done, a user should be able to logout from the server.
On entering invalid credentials, the user is prompted to retry. After 3 consecutive failed attempts,
the user is blocked for a duration of block_duration seconds (block_duration is a command line
argument supplied to the server), and cannot login during this duration (even from another IP
address). While a user is online, if someone uses the same username/password to log in (even from
another IP address), then this new login attempt is denied. If the username is invalid then block
access from that IP address for block_duration.
Timeout - The server should keep track of all online users. If the server does not receive any
commands from a user for a period of timeout seconds (timeout is a command line argument
supplied to the server), then the server should automatically log this user out. Note that, to be
considered active, a user must actively issue a command. The receipt of a message does not count.
Presence Broadcasts - The server should notify the presence/absence of other users logged into the
server, i.e. send a broadcast notification to all online users when a user logs in and logs out.
List of online users - The server should provide a list of users that are currently online in response
to such a query from a user.
Online history – The sever should provide a list of users that logged in for a user specified time in
the past (e.g. users who logged in within the past 15 minutes).
Message Forwarding - The server should forward each instant message to the correct recipient
assuming they are online.
3
Offline Messaging - When the recipient of a message is not logged in (i.e. is offline), the message
will be saved by the server. When the recipient logs in next, the server will send all the unread
messages stored for that user (timestamps are not required).
Message Broadcast – The server should allow a user to broadcast a message to all online users.
Offline messaging is not required for broadcast messages.
Blacklisting - The server should allow a user to block / unblock any other user. For example, if user
A has blocked user B, B can no longer send messages to A i.e. the server should intercept such
messages and inform. B that the message cannot be forwarded. Blocked users also do not get
presence notifications i.e. B will not be informed each time A logs in or logs out.
4.2. Client
The client has the following responsibilities -
Authentication - The client should provide a login prompt to enable the user to authenticate with
the server.
Message - The client should allow the user to send a message to any other user and display
messages sent by other users. The client should also allow the user to send a broadcast message to
all online users.
Notifications - The client should display presence notifications sent by the server about users
logging in and out from the server.
Find users online - The client should provide a way for the user to obtain a list of all the users
currently online from the server.
Find online history – The client should provide a way for the user to obtain a list of all users who
had logged in within a user specified time period.
Blacklist – The client should allow a user to block a user from sending any further messages. The
client should also allow a user to unblock a user that was earlier blocked.
4.3 Commands supported by the client
After a user is logged in, the client should support all the commands shown in the table below. For
the following, assume that commands were run by user A.
Command Description
message Send to through the server. If the user is
online then deliver the message immediately, else store the
message for offline delivery. If has blocked A, then a
message to that effect should be displayed for A. If the is
not present in the credentials file (i.e. invalid user) or is self (A)
then an appropriate error message should be displayed. The
used in our tests will be a few words at most.
broadcast Send to all online users except A and those users who
have blocked A. Inform. A that message could not be sent to some
4
recipients.
whoelse This should display the names of all users that are currently
online excluding A. Users can be displayed in any order.
whoelsesince This should display the names of all users who were logged in at
any time within the past seconds excluding A. Note that
this, may include users that may currently be offline. If is
greater than the time since when the server has been running, then
all users who logged in since the sever initiation time should be
listed. This suggests that you will need to keep a login history
since the start of the server. Users can be displayed in any order.
block blocks the from sending messages to A. A message should
be displayed to A confirming the blocking action. If is self
(i.e., A) or is invalid, then an appropriate error message should be
displayed. must not be informed that A has blocked them.
unblock unblocks the who has been previously blocked by A. A
message should be displayed to A confirming the unblocking
action. If is self (i.e., A) or is invalid or was not already
blocked, then an appropriate error message should be displayed.
logout log out user A.
Any command that is not listed above should result in an error message being displayed to the user.
The interaction with the user should be via the terminal (i.e. console). All messages must be
displayed in the same terminal. There is no need to create separate terminals for messaging with
different users.
We do not mandate the exact text that should be displayed by the client to the user for the various
messages. However, you must make sure that the displayed text is easy to comprehend. Please
make sure that you DO NOT print any debugging information on the client terminal.
We also prefer that you do not print anything at the terminal running the server. We suggest that
you use an optional debug flag (e.g. –d) for the server. When this flag is turned on, your server can
print debugging information to the terminal.
Some examples illustrating client server interaction using the above commands are provided in
Section 10.
4.4 File Names Execution
The main code for the server and client should be contained in the following files: server.c, or
Server.java or server.py, and client.c or Client.java or client.py. You are
free to create additional files such as header files or other class files and name them as you wish.
The server should accept the following three arguments:
• server_port : this is the port number which the server will use to communicate with the
clients. Recall that a TCP socket is NOT uniquely identified by the server port number. So it
is possible for multiple TCP connections to use the same server-side port number.
5
• block_duration : this is the duration in seconds for which a user should be blocked after
three unsuccessful authentication attempts.
• timeout : this is the duration in seconds of inactivity after which a user is logged off by the
server.
The server should be executed before any of the clients. It should be initiated as follows:
If you use Java:
java Server server_port block_duration timeout
If you use C:
./server server_port block_duration timeout
If you use Python:
python server.py server_port block_duration timeout
The client should accept the following two arguments:
• server_IP : this is the IP address of the machine on which the server is running.
• server_port : this is the port number being used by the server. This argument should be
the same as the first argument of the server.
Note that, you do not have to specify the port to be used by the client. You should allow the OS to
pick a random available port. Each client should be initiated in a separate terminal as follows:
If you use Java:
java Client server_IP server_port
If you use C:
./client server_IP server_port
If you use Python:
python client.py server_IP server_port
Note: When you are testing your assignment, you can run the server and multiple clients on the
same machine on separate terminals. In this case, use 127.0.0.1 (local host) as the server IP address.
5. Extended Version
Some users may prefer to have some privacy during messaging. They may want to message their
friends directly without all their conversation being routed via the server. A peer-to-peer messaging
client is a good solution for this scenario. In addition to the above functionalities, the extended
version should implement peer-to-peer messaging (also referred to as private messaging).
The client for the extended version should support the following commands (in addition to those
listed in Section 4.3)
Command Description
startprivate This command indicates that user A wishes to commence p2p
messaging with . The client should obtain the IP address
and port number being used by the from the server. The
client should establish a TCP connection to this IP address and
port number combination. A confirmation message should be