270 likes | 419 Views
Project 2 Internet Relay Chat (IRC). NTU CSIE Computer Networks 2011 Spring. 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. System Structure. Server.
E N D
Project 2 Internet Relay Chat (IRC) NTU CSIE Computer Networks 2011 Spring
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
System Structure Server Reject if the server is too busy Server/client connection P2P connection for file transfer
Scenario 1- Broadcast text messages Server 1. Send msg 2. Broadcast msg
Scenario 2- Exchange files Server 4. Forward the answer and C’s ip:port 2. Ask if user C wants to receive it 1. Request to send file to user C A 5. Create p2p connectionand transfer the file C B 3. Respond to the server
Goal 1: Build a Listening Socket and Use Select() • Create a listening socket • Create a socket queue with size 10 • Use select() to handle incoming connection • If the queue is already overflow, notify the client and close the connection • Lookup your own IP address • Run on a machine with a public IP • Hint: Use “ioctl(int socket, SIOCGIFADDR, struct ifreq) • Bind the interface via an availableport • Randomly generate your own port (larger than 1024) • Bind() via the selected port • If the address is already used, repeat the above procedure
Goal 2: Login to IRC Server • Create a client socket • Read user id • Reply your IP address and port to the server you can download the execution file of server in the attached file
Goal 3: Send Text Message to IRC Server • Read messages from stdin • Send the message to the server • Print the received message
Goal 4: File Transfer • Send a file to the other user • 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 • Sender: create a socket to connect to the receiver and forward the file • Receiver: handle the incoming connection to receive the data
Protocol • Message Format • Login • Terminate • Message broadcast • File transfer
Protocol:Message Format • Massage size limit: 1024 • 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:Control Message (1) #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 (2) #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"
client server Protocol:Login 1. connect(host, port) 2. Close sock REJECT_OVERFLOW • Connect() to the server • Bsd6.csie.ntu.edu.tw port 5000 • Print “the server is too busy” and close the socket • Send login id as getting ACCEPT_LOGIN • NICKNAMEid;USER_MSGLOGIN_ID • Ex: “__NICKNAME__test;__USER_MSG____LOGIN_ID__EOT” • Re-select id as getting REJECT_DUPID or REJECT_EMPTYID • Report the address and port • NICKNAMEid:USER_MSGREPLY_ADDRid:port • ex.: “__NICKNAME__test;__USER_MSG____REPLY_ADDR__140.112.22.22:5001EOT” 1. connect(host, port) ACCEPT_LOGIN 3. LOGIN_ID REJECT_DUPID || REJECT_EMPTYID 4. LOGIN_ID REQUEST_ADDR 5. REPLY_ADDR
Protocol:Terminate client server client • Type /quit or ctrl-c • #include <signal.h> • Signal(SIGINT, yourfunction); • Send ctl message TERMINATE • NICKNAMEmyid;USER_MSGTERMINATE • Ex: “__NICKNAME__myid;__USER_MSG____TERMINATE__EOT” • Receive TERMINATE • Print “userid: has left” • Receive TERMINATE from the server • Close all sockets, and terminate the program 1. /quit or ctrl-c 2. TERMINATE broadcast 3. Print msg 4. Close all sock TERMINATE
Protocol:Message Broadcast client server client • Read the message from stdin • Exclude “/quit” • Send the message • NICKNAMEmyid;USER_MSGmsg • Ex: “__NICKNAME__myid;__USER_MSG__hihi…EOT” • Receive the message • Print the message userid: message 1. Read message 2. Send broadcast 3. recv
Protocol:File Transfer (1) sender server receiver • 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 1. Type cmd 2. REQUEST_FTRAN RESPOND_FTRAN 4. Get ip:port 3. ACCEPT_FTRAN || REJECT_FTRAN ACCEPT_FTRAN || REJECT_FTRAN 5. Create connection All messages should include the tail “EOT”
Protocol:File Transfer (2) sender server receiver • Sender receives • Accept by receiver • NICKNAMEserver;USER_MSGACCEPT_FTRAN recvid FILENAME filename CONNSEQ conn_id ADDRESS ip:port • Reject by receiverNICKNAMEserver;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 1. Type cmd 2. REQUEST_FTRAN RESPOND_FTRAN 4. Get ip:port 3. ACCEPT_FTRAN || REJECT_FTRAN ACCEPT_FTRAN || REJECT_FTRAN 5. Create connection
Protocol:File Transfer (3) sender server receiver • sender: send file size to the receiver • NICKNAMEsendid;USER_MSGRESPNOND_FTRAN sendid FILENAME filename FILESIZE size • Send the file • Sender: Send() • Receiver: Recv() and save as a new file • 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 5. FILESIZE 6. Send the file 7. Close connection
Protocol:File Transfer (4) Handle new connection connection = accept(listen_sock, NULL, NULL) if (queue is not overflowed) sock_list[i] = connection else { 1. send msg: NICKNAMEid;USER_MSGREJECT_OVERFLOW2. close connection }
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
Intro. to Socket Programming • Socket programming
Score • Basic function – 60% • Manage connection: 5% • Error handling: 5% • Text: 20% • 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% • Demo 10%
File format • Max. 2 people in one group • Please tell TA your group by mail:freetempo17@gmail.com • 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 our FTP server
Deadline • 2011/5/27 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.
References • Beej's Guide to Network Programming • http://beej.us/guide/bgnet/output/html/multipage/index.html • Linux cross reference • http://lxr.linux.no/
Demo • 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