Socket Programming Present: KS Wu
2003/10/6NTUEECS COBRA LAB 2 Outline UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6NTUEECS COBRA LAB 3 Outline UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6NTUEECS COBRA LAB 4 close() sendto() recvfrom() socket() bind() recvfrom() sendto() UDP Client UDP Server Wait for a request from client Process request Socket functions for UDP client-server
2003/10/6NTUEECS COBRA LAB 5 Outline UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6NTUEECS COBRA LAB 6 Socket functions socket() bind() recvfrom() sendto() closesocket()
2003/10/6NTUEECS COBRA LAB 7 socket Function int socket( int af, int type, int protocol ); Returns: nonnegative descriptor if OK, negative number on error sd = socket(AF_INET, SOCK_DGRAM, 0)
2003/10/6NTUEECS COBRA LAB 8 Parameters familyDescription AF_INETIPv4 protocols AF_INET6IPv6 protocols typeDescription SOCK_STREAMstream socket (TCP) SOCK_DGRAMdatagram socket (UDP) protocolDescription 0Normally set to 0
2003/10/6NTUEECS COBRA LAB 9 example int sd; sd = socket(AF_INET, SOCK_DGRAM, 0);
2003/10/6NTUEECS COBRA LAB 10 bind Function int bind( SOCKET sockfd, const struct sockaddr* name, int namelen ); Returns: 0 if OK, negative number on error
2003/10/6NTUEECS COBRA LAB 11 Parameters sockfd Descriptor identifying an unbound socket. (returned by the socket function.) name A pointer to a protocol-specific address namelen Length of the value in the name parameter, in bytes.
2003/10/6NTUEECS COBRA LAB 12 example struct sockaddr_in serv; serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl(INADDR_ANY); serv.sin_port = htons(3000); bind(serv_sd, &serv, sizeof(serv));
2003/10/6NTUEECS COBRA LAB 13 recvfrom Function int recvfrom( SOCKET sockfd, char* buf, int len, int flags, struct sockaddr* from, int* fromlen ); Returns: # of bytes received (< len) if OK, 0 if connection has been gracefully closed, negative number on error
2003/10/6NTUEECS COBRA LAB 14 Parameters sockfd Descriptor identifying a bound socket. buf Buffer for the incoming data. len Length of the data in buf, in bytes.
2003/10/6NTUEECS COBRA LAB 15 Parameters (cont.) flags Indicator specifying the way in which the call is made. (usually set to 0) from Optional pointer to a buffer in a sockaddr structure that will hold the source address upon return. fromlen Optional pointer to the size, in bytes, of the from buffer.
2003/10/6NTUEECS COBRA LAB 16 example char mesg[MAXLINE]; n = recvfrom(sd, mesg, MAXLINE, 0, &cli, &cli_len);
2003/10/6NTUEECS COBRA LAB 17 sendto Function int sendto( SOCKET sockfd, const char* buf, int len, int flags, const struct sockaddr* to, int tolen ); Returns: # of bytes sent (< len) if OK, negative number on error
2003/10/6NTUEECS COBRA LAB 18 Parameters sockfd Descriptor identifying a bound socket. buf Buffer containing the data to be transmitted. len Length of the data in buf, in bytes.
2003/10/6NTUEECS COBRA LAB 19 Parameters (cont.) flags Indicator specifying the way in which the call is made. (usually set to 0) to Optional pointer to a sockaddr structure that contains the address of the target socket. tolen Size of the address in to, in bytes.
2003/10/6NTUEECS COBRA LAB 20 example sendto(sd, mesg, n, 0, &cli, cli_len);
2003/10/6NTUEECS COBRA LAB 21 closesocket Function int closesocket( SOCKET sockfd ); Returns: 0 if OK, negative number on error
2003/10/6NTUEECS COBRA LAB 22 Parameters sockfd Descriptor identifying the socket to close.
2003/10/6NTUEECS COBRA LAB 23 example closesocket(sd);
2003/10/6NTUEECS COBRA LAB 24 Outline UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6NTUEECS COBRA LAB 25 How MSN Messenger works 2 phases Authentication Phase Instant Messaging Phase
2003/10/6NTUEECS COBRA LAB 26 How MSN Messenger works (cont.) Authentication Phase logging into the MSN messenger server Retrieve the friend list Instant Messaging Phase Session-based sending/accepting requests for an Instant Messaging session sending/receiving messages
2003/10/6NTUEECS COBRA LAB 27 Server Components Dispatch Server Notification Server Switchboard Server
2003/10/6NTUEECS COBRA LAB 28 Dispatch server protocol version negotiation determination of which NS is associated with the client making a connection referring the client to the proper NS
2003/10/6NTUEECS COBRA LAB 29 Notification server authenticate, synchronize user properties exchange asynchronous event notifications
2003/10/6NTUEECS COBRA LAB 30 Switchboard server provide instant messaging sessions
2003/10/6NTUEECS COBRA LAB 31 Scenario DS Client 1. 確定版本 TCP (port 1863) version version! 2. 確定加密演算法 Policy?SP (MD5) 3. 認證使用者 Initiate infoChallenge infoPasswd + challenge userID + nickname
2003/10/6NTUEECS COBRA LAB 32 Scenario DS Client 4. 將 user 導到 NS NS addr:port NS Log in 5. 同步化使用者資訊 ( 更新 ) Latest properties? (cache) Yes No, update! 6. 下載新版 friend list List? List nickname nickname …
2003/10/6NTUEECS COBRA LAB 33 Scenario Client NS 7. 使用者狀態 State Online Offline Invisible OK! 8. 修改 friend list OK! (new SN) ADD/REM
2003/10/6NTUEECS COBRA LAB 34 Scenario Client A NS SS? 將 user 導到 SS SS addr SP (CHI) cookie 連結 SS 及做認證 SS userID cookie OK! 邀請使用者加入對談的 session I want to talk with B! SessionID 建立對談 Client B SessionID SS addr SP Cookie IDA nickname IDB cookie sessionID Index, # of participants 送出及時訊息 Hello!
2003/10/6NTUEECS COBRA LAB 35 Scenario Client A NSSS Client B 變更 Session 參與者 JOIN! Client C New userID nickname Left! Left userID Bye!
2003/10/6NTUEECS COBRA LAB 36 Download
2003/10/6NTUEECS COBRA LAB 37 Reference “Unix Network Programming” “WinSock 網路程式設計之鑰 ” aft.php aft.php
Thank You!