380 likes | 453 Views
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.
E N D
Socket Programming Present: KS Wu
Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB
Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB
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
Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB
Socket functions • socket() • bind() • recvfrom() • sendto() • closesocket() NTUEECS COBRA LAB
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
Parameters NTUEECS COBRA LAB
example int sd; sd = socket(AF_INET, SOCK_DGRAM, 0); NTUEECS COBRA LAB
bind Function int bind( SOCKET sockfd, const struct sockaddr* name, int namelen ); Returns: 0 if OK, negative number on error NTUEECS COBRA LAB
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
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
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
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
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
example char mesg[MAXLINE]; n = recvfrom(sd, mesg, MAXLINE, 0, &cli, &cli_len); NTUEECS COBRA LAB
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
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
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
example sendto(sd, mesg, n, 0, &cli, cli_len); NTUEECS COBRA LAB
closesocket Function int closesocket( SOCKET sockfd ); Returns: 0 if OK, negative number on error NTUEECS COBRA LAB
Parameters • sockfd • Descriptor identifying the socket to close. NTUEECS COBRA LAB
example closesocket(sd); NTUEECS COBRA LAB
Outline • UDP client/server communication • Introduction of socket functions • How MSN Messenger works NTUEECS COBRA LAB
How MSN Messenger works • 2 phases • Authentication Phase • Instant Messaging Phase NTUEECS COBRA LAB
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
Server Components • Dispatch Server • Notification Server • Switchboard Server NTUEECS COBRA LAB
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
Notification server • authenticate, synchronize user properties • exchange asynchronous event notifications NTUEECS COBRA LAB
Switchboard server • provide instant messaging sessions NTUEECS COBRA LAB
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
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
Online Offline Invisible State Client Scenario 7. 使用者狀態 8. 修改friend list NS ADD/REM xx@xxx.com nickname OK! (new SN) OK! NTUEECS COBRA LAB
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
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
Download • http://cobra.ee.ntu.edu.tw/~fku/temp.htm NTUEECS COBRA LAB
Reference • “Unix Network Programming” • “WinSock 網路程式設計之鑰” • http://msdn.microsoft.com • http://www.hypothetic.org/docs/msn/ietf_draft.php NTUEECS COBRA LAB