1 / 38

Socket Programming

Socket Programming. Present: KS Wu. Outline. UDP client/server communication Introduction of socket functions How MSN Messenger works. Outline. UDP client/server communication Introduction of socket functions How MSN Messenger works. UDP Server. Socket functions for UDP client-server.

Download Presentation

Socket Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming Present: KS Wu

  2. Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB

  3. Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB

  4. UDP Server Socket functions for UDP client-server socket() bind() UDP Client socket() recvfrom() Wait for a request from client sendto() Process request sendto() recvfrom() close() NTUEECS COBRA LAB

  5. Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB

  6. Socket functions • socket() • bind() • recvfrom() • sendto() • closesocket() NTUEECS 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) NTUEECS COBRA LAB

  8. Parameters NTUEECS COBRA LAB

  9. example int sd; sd = socket(AF_INET, SOCK_DGRAM, 0); NTUEECS COBRA LAB

  10. bind Function int bind( SOCKET sockfd, const struct sockaddr* name, int namelen ); Returns: 0 if OK, negative number on error NTUEECS 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. NTUEECS 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)); NTUEECS 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 NTUEECS 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. NTUEECS 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. NTUEECS COBRA LAB

  16. example char mesg[MAXLINE]; n = recvfrom(sd, mesg, MAXLINE, 0, &cli, &cli_len); NTUEECS 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 NTUEECS 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. NTUEECS 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. NTUEECS COBRA LAB

  20. example sendto(sd, mesg, n, 0, &cli, cli_len); NTUEECS COBRA LAB

  21. closesocket Function int closesocket( SOCKET sockfd ); Returns: 0 if OK, negative number on error NTUEECS COBRA LAB

  22. Parameters • sockfd • Descriptor identifying the socket to close. NTUEECS COBRA LAB

  23. example closesocket(sd); NTUEECS COBRA LAB

  24. Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB

  25. How MSN Messenger works • 2 phases • Authentication Phase • Instant Messaging Phase NTUEECS 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 NTUEECS COBRA LAB

  27. Server Components • Dispatch Server • Notification Server • Switchboard Server NTUEECS 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 NTUEECS COBRA LAB

  29. Notification server • authenticate, synchronize user properties • exchange asynchronous event notifications NTUEECS COBRA LAB

  30. Switchboard server • provide instant messaging sessions NTUEECS COBRA LAB

  31. Client Scenario 1. 確定版本 2. 確定加密演算法 DS 3.認證使用者 TCP (port 1863) version userID + nickname Challenge info SP (MD5) version! Policy? Initiate info Passwd + challenge NTUEECS COBRA LAB

  32. Yes No, update! Client Scenario 4. 將user導到NS 5. 同步化使用者資訊 (更新) 6. 下載新版friend list DS NS NS addr:port Latest properties? (cache) List? List aa@aaa.com nickname b@bbb.com nickname … Log in NTUEECS COBRA LAB

  33. Online Offline Invisible State Client Scenario 7. 使用者狀態 8. 修改friend list NS ADD/REM xx@xxx.com nickname OK! (new SN) OK! NTUEECS COBRA LAB

  34. Client B Client A Scenario 建立對談 邀請使用者加入對談的session SessionID SS addr SP Cookie IDA nickname IDB cookie sessionID Hello! Index, # of participants NS SS 連結SS及做認證 將user導到SS SessionID OK! SS? SS addr SP (CHI) cookie Hello! userID cookie I want to talk with B! 送出及時訊息 NTUEECS COBRA LAB

  35. Client B Client C Client A Scenario 變更Session參與者 JOIN! Left! New userID nickname Left userID Bye! NS SS New userID nickname Left userID NTUEECS COBRA LAB

  36. Download • http://cobra.ee.ntu.edu.tw/~fku/temp.htm NTUEECS COBRA LAB

  37. Reference • “Unix Network Programming” • “WinSock 網路程式設計之鑰” • http://msdn.microsoft.com • http://www.hypothetic.org/docs/msn/ietf_draft.php NTUEECS COBRA LAB

  38. Thank You!

More Related