In this homework, you are required to write two programs, a client and a server, to build a chat room system. The clients talk to one another through the server. The server accepts connections from the clients and processes the command from the clients.
Hint:It is suggested that you use select() for constructing the client and the server programs.
Specification
Server
- The command to start the server./server <SERVER PORT>
If the number of arguments is not one, the server program should terminates.The server can serve multiple clients simultaneously. Once a connection is established, the server will send a hello message to the client and give the client a username anonymous. The client can send different commands to the server. The messages transmitted between clients and the server are shown below.
Hello Message
When a client connects to the server, the server will send a hello message to the client and then broadcasts this users presence to other clients.
- Client output format
- To the newly connected client[Server] Hello, anonymous! From: <Client IP>:<Client Port>
- To existing clients[Server] Someone is coming!
Offline Message
When a client disconnect from the server, the server will send an offline message to all the other online clients to tell them someone has been offline.
- Client output format[Server] <USERNAME> is offline.
Who Message
A client can type command below to list all online users.
- Client input formatwho
The server will reply to sender a list of online users and tag the sender client. For N user, Server will send N lines. Each of them shows details of a user.
- Client output format
- If the user is not the client itself[Server] <USERNAME> <CLIENT IP>:<CLIENT PORT>
- If the user is the client itself[Server] <SENDER USERNAME> <CLIENT IP>:<CLIENT PORT> ->me
Change Username Message
A client can type the command below to change his/her username.
- Client input formatname <NEW USERNAME>
The server has to verify if the new name is valid which means the input name is
- not anonymous,
- unique, and
- 2~12 English letters.
It will reject users request if this name cannot fit the rule.
- Client output format
- If the new name is anonymous.[Server] ERROR: Username cannot be anonymous.
- If the new name is not unique.[Server] ERROR: <NEW USERNAME> has been used by others.
- If the new name does not consist of 2~12 English letters.[Server] ERROR: Username can only consists of 2~12 English letters.
The server will reply some messages to all users once a user changes his/her name.
- Client output format
- To user which changed his/her name[Server] Youre now known as <NEW USERNAME>.
- To other users[Server] <OLD USERNAME> is now known as <NEW USERNAME>.
Note:A user can be rename as itself, that is, when userA wants to rename as userA, server should not return error messages.
Private Message
A client can send a private message to a specific client.
- Client input formattell <USERNAME> <MESSAGE>
The server will send an error message back to the sender if either the senders name or the receivers name is anonymous.
- Client output format
- If the senders name is anonymous[Server] ERROR: You are anonymous.
- If the receivers name is anonymous[Server] ERROR: The client to which you sent is anonymous.
- If the receiver doesnt exist[Server] ERROR: The receiver doesnt exist.
Otherwise, the server sends the private message to the specific client and sends back a notification to the sender.
- Client output format
- To the sender: If the message is sent[Server] SUCCESS: Your message has been sent.
- To the receiver: If both clients name are not anonymous[Server] <SENDER USERNAME> tell you <MESSAGE>
Broadcast Message
A client can send a broadcast message to all clients.
- Client input formatyell <MESSAGE>
While receiving the command from a user, the server will add <SENDERs USERNAME> at the head of it and broadcasts to all users including the sender.
- Client output format[Server] <SENDERs USERNAME> yell <MESSAGE>
Error Command
Commands which havent been declared above are invalid commands. When a server receives an invalid command, it should send an error message back to the sending client.
- Client output format[Server] ERROR: Error command.
Client
A client cannot connect to more than one server at the same time.Users should give the server IP and the port as the arguments of the client program.If the number of arguments is not two, the client program should terminates.
- The command to start the client./client <SERVER IP> <SERVER PORT>
Exit Command
The user can type the command below to terminate the process at any time.
- Command Formatexit
Note:This command should be process by the client locally. That is, the client should close the connection and terminate the process while it receives the exit command.
Receive & Display Format
The clients keep receiving commands from stdin and, except for exit command, send those to the server directly without any modification.
Note:For messages received from stdin, client can only process the exit command, others should be sent to the server without modification. All commands received from stdin (except exit command) should be sent to server directly.
Reviews
There are no reviews yet.