C代写:CS240 IRC Server

代写IRC(Internet Relay Chat), 熟悉Linux下的网络编程。

Before the Lab

Study in the slides the C++ material

Step 1. Setting up Your Environment

Remote login to data by typing:

ssh <your-user-name>@data.cs

Change to the directory ~/cs240 that you created previously.

cd
cd cs240

Now copy the initial lab10 files by typing:

tar -xvf /homes/cs240/lab10-irc-server/lab10-src.tar
cd lab10-src

Step 2. Using Telnet as a Tool to Debug Network Applications

The telnet program was initially implemented to connect to computers remotely. Since telnet passes the passwords as plain text through the network, ssh is now used instead of telnet to connect to computers remotely. Telnet is still used to debug network applications. Telnet uses the following arguments:

telnet <host> <port>

where <host> is the name of the host and the <port> is the port number that the server uses. A <port> is a number that identifies an endpoint program in the server.

For example run the following command to connect to a time server:

$telnet time-A.timefreq.bldrdoc.gov 13       ← Type this
Trying 132.163.4.101...
Connected to time-A.timefreq.bldrdoc.gov.
Escape character is '^]'.

57112 15-03-31 01:57:32 50 0 0 360.9 UTC(NIST) *
Connection closed by foreign host.

Also, you may use telnet to connect to a HTTP server:

$ telnet google.com 80 ← Type this
Trying 74.125.225.0...
Connected to google.com.
Escape character is '^]'.
GET / HTTP/1.0      ← Type this without back spaces and then type enter twice.

HTTP/1.0 200 OK   ← This is the HTTP response with the initial HTML document.
Date: Tue, 31 Mar 2015 01:59:09 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
Set-Cookie: PREF=ID=3ad8cbe7ea26c3f7:FF=0:TM=1427767149:LM=1427767149:S=Y3nU9IMxxYhPNmNt; expires=Thu, 30-Mar-2017 01:59:09 GMT; path=/; domain=.google.com

You also may use telnet to connect and debug to other network applications.

In this step use these two telnet commands to connect to a time server and a HTTP server.

You will also use telnet to test your IRC server before you implement the IRC client.

Step 3. Running the Initial Server

After typing make run the IRCServer. Choose for a port a random number 1024 < port < 65536.

In one window run the server and type:

$ ./IRCServer 2030

In a separate window run telnet to connect to the server.

$ telnet data.cs 2030
ADD-USER peterparker abc123 ← Type a command and arguments.

The server will display:

$ ./IRCServer 2030
RECEIVED: ADD-USER peterparker abc123
The commandLine has the following format:
COMMAND <user> <password> <arguments>. See below.
You need to separate the commandLine into those components
For now, command, user, and password are hardwired.
command=ADD-USER
user=peter
password=spider
args=

You will need to parse the line received and separate it into user, password and argument.Then you will implement the different functions that make the IRC server.

Step4. Implementing the IRC Server

The IRC server will have multiple chat rooms and the users can enter a room and type messages. The messages will be seen by all the users in the room. Only users who have entered a room can post messages in the room.

Here is a typical conversation with the server.

In one window run the IRC server. Choose a different port number since it has to be unique across other programs using sockets in the same machine.

$ ./IRCServer 2030

In another window connect to the server. You could also run the telnet command in a completely different machine or even from a laptop that has telnet/putty installed.

$telnet data.cs 2030
ADD-USER superman clarkkent← Type this to add a user “peter” with password “abcabc”
OK ← Server sends back OK
Connection closed by foreign host.

$telnet data.cs 2030
ADD-USER spiderman peterparker
OK
Connection closed by foreign host.

$telnet data.cs 2030
GET-ALL-USERS superman clarkkent
superman
spiderman
Connection closed by foreign host.

You will implement the following functions in the server:

Request: ADD-USER <USER> <PASSWD>\r\n
Answer: OK\r\n or DENIED\r\n

Request: GET-ALL-USERS <USER> <PASSWD>\r\n
Answer: USER1\r\n
        USER2\r\n
        ...
        \r\n
        or
        DENIED\r\n

Request: CREATE-ROOM <USER> <PASSWD> <ROOM>\r\n
Answer: OK\n or DENIED\r\n

Request: LIST-ROOMS <USER> <PASSWD>\r\n
Answer: room1\r\n
        room2\r\n
        ...
        \r\n
        or
        DENIED\r\n

Request: ENTER-ROOM <USER> <PASSWD> <ROOM>\r\n
Answer: OK\n or DENIED\r\n

Request: LEAVE-ROOM <USER> <PASSWD> <ROOM>\r\n
Answer: OK\n or DENIED\r\n

Request: SEND-MESSAGE <USER> <PASSWD> <ROOM> <MESSAGE>\n
Answer: OK\n or DENIED\n

Request: GET-MESSAGES <USER> <PASSWD> <LAST-MESSAGE-NUM> <ROOM>\r\n
Answer: MSGNUM1 USER1 MESSAGE1\r\n
        MSGNUM2 USER2 MESSAGE2\r\n
        MSGNUM3 USER2 MESSAGE3\r\n
        ...\r\n
        \r\n
        or
        DENIED\r\n

Request: GET-USERS-IN-ROOM <USER> <PASSWD> <ROOM>\r\n
Answer: USER1\r\n
        USER2\r\n
        …
        \r\n
        or
        DENIED\r\n

You will have to test your server using the telnet command.

Your chat room needs to implement the following constraints:

  • Usernames are unique
  • Room names are unique
  • Only users in the room can post messages.
  • User needs password for all operations
  • Only a maximum of 100 messages are saved in any room. Newer messages will replace older messages.

You are welcome to use in this project strings, hashmaps, vectors etc from the C++ STL (Standard Template Library).

In lab11 you will implement the client program that will talk to the IRCServer.

Step 5. Turning In your Project

Follow these instructions to turn in lab10:

Login to data.cs and type
cd cs240
turnin -c cs240 -v -p lab10 lab10-src