socket programming
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
Socket Programming
Present: KS Wu
2003/10/6 NTUEECS COBRA LAB 2
Outline
UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6 NTUEECS COBRA LAB 3
Outline
UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6 NTUEECS COBRA LAB 4close()
sendto()
recvfrom()
socket()
socket()
bind()
recvfrom()
sendto()
UDP Client
UDP Server
Wait for a request from
client
Process request
Socket functions for UDP client-server
2003/10/6 NTUEECS COBRA LAB 5
Outline
UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6 NTUEECS COBRA LAB 6
Socket functions
WSAStartup( )
socket( )bind( )sendto( )recvfrom( )closesocket( )
2003/10/6 NTUEECS COBRA LAB 7
WSAStartup Function
WSADATA wsadata;
WSAStartup(0x101, (LPWSADATA) &wsadata)
version: 1.1
2003/10/6 NTUEECS COBRA LAB 8
socket Function
int socket( int af, int type, int protocol );
Returns: nonnegative descriptor if OK, negative number on error
2003/10/6 NTUEECS COBRA LAB 9
Parameters
family DescriptionAF_INET IPv4 protocolsAF_INET6 IPv6 protocols
type DescriptionSOCK_STREAM stream socket (TCP)SOCK_DGRAM datagram socket (UDP)
protocol Description0 Normally set to 0
2003/10/6 NTUEECS 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/6 NTUEECS COBRA LAB 11
Parameters
sockfd Descriptor identifying an unbound socket. (retur
ned by the socket function.)name
A pointer to a protocol-specific addressnamelen
Length of the value in the name parameter, in bytes.
2003/10/6 NTUEECS COBRA LAB 12
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/6 NTUEECS COBRA LAB 13
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/6 NTUEECS COBRA LAB 14
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/6 NTUEECS COBRA LAB 15
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/6 NTUEECS COBRA LAB 16
Parameters
sockfd Descriptor identifying a bound socket.
buf Buffer for the incoming data.
len Length of the data in buf, in bytes.
2003/10/6 NTUEECS COBRA LAB 17
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/6 NTUEECS COBRA LAB 18
closesocket Function
int closesocket( SOCKET sockfd );
Returns: 0 if OK, negative number on error
2003/10/6 NTUEECS COBRA LAB 19
Parameters
sockfd Descriptor identifying the socket to close.
2003/10/6 NTUEECS COBRA LAB 20
Outline
UDP client/server communication Introduction of socket functions How MSN Messenger works
2003/10/6 NTUEECS COBRA LAB 21
How MSN Messenger works
2 phases Authentication Phase Instant Messaging Phase
2003/10/6 NTUEECS COBRA LAB 22
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/6 NTUEECS COBRA LAB 23
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
2003/10/6 NTUEECS COBRA LAB 24
Instant Messaging Phase
Referral to switchboardSwitchboard connections and
authenticationInviting users to a switchboard sessionSession participant changesLeaving a switchboard sessionInstant messagesReceiving an instant message
2003/10/6 NTUEECS COBRA LAB 25
Server Components
Dispatch serverNotification serverSwitchboard server
2003/10/6 NTUEECS COBRA LAB 26
Dispatch server
protocol version negotiationdetermination of which NS is associated
with the client making a connectionreferring the client to the proper NS
2003/10/6 NTUEECS COBRA LAB 27
Notification server
authenticate, synchronize user propertiesexchange asynchronous event
notifications
2003/10/6 NTUEECS COBRA LAB 28
Switchboard server
provide instant messaging sessions
2003/10/6 NTUEECS COBRA LAB 29
Scenario
DS
Client
1.Protocol Versioning
TCP (port 1863)version
2.Server policy information
Policy?SP (MD5)
3.Authentication
Initiate infoChallenge info Passwd + challengeuserID + nickname
2003/10/6 NTUEECS COBRA LAB 30
Scenario
DS
Client
4.Referral
NS addr:port
NS
Log in
5.Client User Property Synchronization
Latest properties? (cache)Yes
No, update!
6.List Retrieval And Property Management
List?List
[email protected] nickname
[email protected] nickname
…
2003/10/6 NTUEECS COBRA LAB 31
Scenario
Client
NS
7.Client States
State
Online
Offline
InvisibleOK!
8.List Modifications
OK! (new SN)
ADD/REM [email protected] nickname
2003/10/6 NTUEECS COBRA LAB 32
Scenario
Client
NS
SS?
Referral to Switchboard
SS addr
SP (CHI)
cookie
Switchboard Connections and Authentication
SS
userID cookieOK!
Inviting Users to a Switchboard Session
called userID
Session ID
2003/10/6 NTUEECS COBRA LAB 33
Scenario
Client
NS SS
Getting Invited to a Switchboard Session
SessionID
SS addr
SP
Cookie
callerID nickname
userID cookie sessionID
index
# of participants
Session Participant Changes
New userID nickname
JOIN!Left!
Left userIDBye!
Leaving a Switchboard Session
2003/10/6 NTUEECS COBRA LAB 34
Scenario
Client
NS SS
Sending a instant message
Msg
Receiving an Instant Message
Msg
2003/10/6 NTUEECS COBRA LAB 35
Reference
“Unix Network Programming”“WinSock 網路程式設計之鑰”http://msdn.microsoft.comhttp://www.hypothetic.org/docs/msn/ietf_dr
aft.php
Thanks for attention!