360 likes | 458 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 • WSAStartup( ) • socket( ) • bind( ) • sendto( ) • recvfrom( ) • closesocket( ) NTUEECS COBRA LAB
WSAStartup Function WSADATA wsadata; WSAStartup(0x101, (LPWSADATA) &wsadata) version: 1.1 NTUEECS COBRA LAB
socket Function int socket( int af, int type, int protocol ); Returns: nonnegative descriptor if OK, negative number on error NTUEECS COBRA LAB
Parameters 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
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
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
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
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
Authentication Phase • Protocol versioning • Server policy information • Authentication • Referral • Client user property synchronization • List retrieval and property management • Client states • List modifications • Notification messages • Connection closed NTUEECS COBRA LAB
Instant Messaging Phase • Referral to switchboard • Switchboard connections and authentication • Inviting users to a switchboard session • Session participant changes • Leaving a switchboard session • Instant messages • Receiving an instant message 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 2.Server policy information 1.Protocol Versioning DS 3.Authentication TCP (port 1863) SP (MD5) version Policy? Initiate info Passwd + challenge userID + nickname Challenge info NTUEECS COBRA LAB
Yes No, update! Client Scenario DS 6.List Retrieval And Property Management 5.Client User Property Synchronization 4.Referral 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 8.List Modifications 7.Client States NS ADD/REM xx@xxx.com nickname OK! (new SN) OK! NTUEECS COBRA LAB
Client Scenario Inviting Users to a Switchboard Session Switchboard Connections and Authentication Referral to Switchboard NS SS Session ID OK! SS? SS addr SP (CHI) cookie userID cookie called userID NTUEECS COBRA LAB
Client Scenario Getting Invited to a Switchboard Session Session Participant Changes Leaving a Switchboard Session NS SS Left! JOIN! index # of participants SessionID SS addr SP Cookie callerID nickname New userID nickname Left userID Bye! userID cookie sessionID NTUEECS COBRA LAB
Client Scenario Sending a instant message Receiving an Instant Message NS SS Msg Msg NTUEECS COBRA LAB
Reference • “Unix Network Programming” • “WinSock 網路程式設計之鑰” • http://msdn.microsoft.com • http://www.hypothetic.org/docs/msn/ietf_draft.php NTUEECS COBRA LAB