Presentation is loading. Please wait.

Presentation is loading. Please wait.

NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Similar presentations


Presentation on theme: "NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)"— Presentation transcript:

1 NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

2 Project Goal  In this Project, students are asked to get familiar with building a multi-sockets system  Students are requested to implement a client/server and P2P coexisting system

3 System Structure Server Reject if the server is too busy Server/client connection P2P connection for file transfer

4 Scenario 1 - Broadcast text messages Server 1. Send msg 2. Broadcast msg

5 Scenario 2 - Exchange files Server 1. Request to send file to user C 5. Create p2p connection and transfer the file C A B 2. Ask if user C wants to receive it 3. Respond to the server 4. Forward the answer and C’s ip:port

6 1.Create a listening socket 2.Create a socket queue with size 10 3.Use select() to handle incoming connection  If the queue is already overflow, notify the client and close the connection 4.Lookup your own IP address  Run on a machine with a public IP  Hint: Use “ioctl(int socket, SIOCGIFADDR, struct ifreq) 5.Bind the interface via an available port  Randomly generate your own port (larger than 1024)  Bind() via the selected port  If the address is already used, repeat the above procedure Goal 1: Build a Listening Socket and Use Select()

7 1. Create a client socket ◦ Connect to bsd6.csie.ntu.edu.tw port 5000 2. Read user id 3. Reply your IP address and port to the server Goal 2: Login to IRC Server

8 Goal 3: Send Text Message to IRC Server 1. Read messages from stdin 2. Send the message to the server 3. Print the received message

9 Goal 4: File Transfer 1. Send a file to the other user 2. Negotiate via four-way handshaking ◦ The sender sends a request to the server ◦ The server forwards the request to the receiver ◦ The receiver responses the answer to the server ◦ The server relays the answer along with the IP and port to the sender 3. Sender: create a socket to connect to the receiver and forward the file 4. Receiver: handle the incoming connection to receive the data

10  Message Format  Login  Terminate  Message broadcast  File transfer Protocol

11  Massage size limit: 1024 EOT  NICKNAMEname;USER_MSGmsg + EOT ◦ “__NICKNAME__server;__USER_MSG____ACCEPT_LOGIN__EOT”  Sender: server  Msg: __ACCEPT_LOGIN__ ◦ “__NICKNAME__testuser;__USER_MSG__this is a message EOT”  Sender: testuser  Msg: this is a message  See default message name in irc_util.h ◦ Don’t send messages starting by /quit, /send, /from, /list, /acceptf, /rejectf, /help Protocol: Message Format

12 #define MSG_SIZE 1024 #define CONN_NUM 3 #define NICKNAME "__NICKNAME__" #define LOGIN_ID "__LOGIN_ID__" #define REQUEST_ADDR "__REQUEST_ADDR__" #define REPLY_ADDR "__REPLY_ADDR__" #define REJECT_EMPTYID "__REJECT_EMPTYID__" #define REJECT_DUPID "__REJECT_DUPID__" #define ACCEPT_LOGIN "__ACCEPT_LOGIN__" #define REJECT_OVERFLOW "__REJECT_OVERFLOW__" #define USER_MSG "__USER_MSG__" #define REJECT_FTRAN_NULL_USER "__REJECT_FTRAN_NULL_USER__" #define REJECT_FTRAN_BY_USER "__REJECT_FTRAN_BY_USER__" Protocol: Control Message (1)

13 #define TERMINATE "/quit" #define GETLIST "/list" #define GETHELP "/help" #define REQUEST_FTRAN "/send" #define RESPOND_FTRAN "/from" #define FILENAME "/file" #define FILESIZE "/fsize" #define ADDRESS "/addr" #define CONNSEQ "/connseq" #define ACCEPT_FTRAN "/acceptf" #define REJECT_FTRAN "/rejectf" Protocol: Control Message (2)

14 Protocol: Login clientserver 1. connect(host, port) REJECT_OVERFLOW 3. LOGIN_ID REJECT_DUPID || REJECT_EMPTYID 4. LOGIN_ID REQUEST_ADDR 5. REPLY_ADDR 1.Connect() to the server  Bsd6.csie.ntu.edu.tw port 5000 2.Print “the server is too busy” and close the socket 3.Send login id as getting ACCEPT_LOGIN  NICKNAMEid;USER_MSGLOGIN_ID  Ex: “__NICKNAME__test;__USER_MSG____LOGIN_ID__EOT” 4.Re-select id as getting REJECT_DUPID or REJECT_EMPTYID 5.Report the address and port  NICKNAMEid:USER_MSGREPLY_ADDRid:port  ex.: “__NICKNAME__test;__USER_MSG____REPLY_ADDR__14 0.112.22.22:5001EOT” 2. Close sock 1. connect(host, port) ACCEPT_LOGIN

15 1.Type /quit or ctrl-c  #include  Signal(SIGINT, yourfunction); 2.Send ctl message TERMINATE  NICKNAMEmyid;USER_MSGTERMINATE  Ex: “__NICKNAME__myid;__USER_MSG____TER MINATE__EOT” 3.Receive TERMINATE  Print “userid: has left” 4.Receive TERMINATE from the server  Close all sockets, and terminate the program Protocol: Terminate clientserver 1. /quit or ctrl-c 2. TERMINATE client broadcast 3. Print msg TERMINATE 4. Close all sock

16 1.Read the message from stdin  Exclude “/quit” 2.Send the message  NICKNAMEmyid;USER_MSGmsg  Ex: “__NICKNAME__myid;__USER_MSG__hihi …EOT” 3.Receive the message  Print the message userid: message Protocol: Message Broadcast clientserver 1. Read message 2. Send client broadcast 3. recv

17  Cmd: /send recvid /file filepath ◦ Check if filepath exists ◦ Extract filename from the file path  Sender sends ◦ NICKNAMEsendid;USER_MSGREQUEST_FTRAN recvid FILENAME filename CONNSEQ conn_id  Receiver receives ◦ NICKNAMEserver;USER_MSGRESPOND_FTRAN sendid FILENAME filename CONNSEQ conn_id ◦ Ask if the receiver wants to accept it. (block) ◦ NICKNAMErecvid;USER_MSGACCEPT_FTRAN sendid FILENAME filename CONNSEQ conn_id ◦ NICKNAMErecvid;USER_MSGREJECT_FTRAN sendid FILENAME filename CONNSEQ conn_id Protocol: File Transfer (1) senderserver 2. REQUEST_FTRAN receiver 1. Type cmd RESPOND_FTRAN 3. ACCEPT_FTRAN || REJECT_FTRAN ACCEPT_FTRAN || REJECT_FTRAN 5. Create connection 4. Get ip:port All messages should include the tail “EOT”

18  Sender receives ◦ Accept by receiver ◦ NICKNAMEserver;USER_MSGACCEPT_FTRAN recvid FILENAME filename CONNSEQ conn_id ADDRESS ip:port ◦ Reject by receiver NICKNAMEserver;USER_MSGREJECT_FTRAN recvid FILENAME filename CONNSEQ conn_id REJECT_FTRAN_BY_USER ◦ Reject due to the wrong user name NICKNAMEserver;USER_MSGREJECT_FTRAN recvid FILENAME filename CONNSEQ conn_id REJECT_FTRAN_NULL_USER ◦ Create connection to ip:port Protocol: File Transfer (2) senderserver 2. REQUEST_FTRAN receiver 1. Type cmd RESPOND_FTRAN 3. ACCEPT_FTRAN || REJECT_FTRAN ACCEPT_FTRAN || REJECT_FTRAN 5. Create connection 4. Get ip:port

19 5. sender: send file size to the receiver ◦ NICKNAMEsendid;USER_MSGRESPNOND_FTRA N sendid FILENAME filename FILESIZE size 6. Send the file ◦ Sender: Send() ◦ Receiver: Recv() and save as a new file 7. Close the connection as file transfer is finished or the remote connection is lost  Receiver saves the file in its local directory  Receiver only needs to overwrite the file if the file name already exists ◦ Don’t need to process if the file can not be accessed Protocol: File Transfer (3) senderserverreceiver 5. FILESIZE 6. Send the file 7. Close connection

20 connection = accept(listen_sock, NULL, NULL) if (queue is not overflowed) sock_list[i] = connection else { 1. send msg: NICKNAMEid;USER_MSGREJECT_OVERFLOW 2. close connection } Protocol: File Transfer (4) Handle new connection

21 Requirements  Write your program on R217 workstations ◦ You can not use multi-process. Please use select() to realize the requirements of Project 2  Demo ◦ To be announced

22 Intro. to Socket Programming  Socket programming Socket programming

23 Score  Basic function – 60%  Manage connection: 10%  Text: 30%  File transfer: 30%  Makefile and command – 10% ◦ make clean // clean obj(*.o) and executable ◦ make // compile source code ◦./irc_client hostname server_port// Run client  Bonus  Synchronous file transfer 20%

24 File format  Max. 2 people in one group ◦ Group member is the same as Project 1 ◦ Please tell TA if you want to change your group by mail: cjlin@cmlab.csie.ntu.edu.tw cjlin@cmlab.csie.ntu.edu.tw  Tar your file ◦ tar zcvf bXXXXXXXX_bOOOOOOOO_prj1.tar.gz Makefile file1 file2 … ◦ Use “bXXXXXXXX_prj2.tar.gz” if you do not team with others ◦ DO NOT have any directory in.tar file; all files should be extract to current directory when using “tar zxvf bXXXXXXXX_prj2.tar.gz” ◦ Send your file to http://www.csie.ntu.edu.tw/~artoo/CN2009/hws.htm http://www.csie.ntu.edu.tw/~artoo/CN2009/hws.htm

25 Deadline  2009/5/20 24:00  There WOULD BE penalty for late submission. The penalty for the first day is 10 points, the second day is 20 points, etc. Please turn in your code in time.

26 References  Beej's Guide to Network Programming ◦ http://beej.us/guide/bgnet/output/html/multipage /index.html http://beej.us/guide/bgnet/output/html/multipage /index.html  Linux cross reference ◦ http://lxr.linux.no/ http://lxr.linux.no/

27  Login ◦ Normal login ◦ Dup id  Server is busy  Text message  File transfer ◦ Accept request ◦ Reject request ◦ Request, but the recv id is wrong ◦ Request, but the other side is gone ◦ The remote site leaves under transmission ◦ Normal transmission  Terminate by /quit, ctrl-c, server Demo


Download ppt "NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)"

Similar presentations


Ads by Google