socket programming

36
Socket Programming Present: KS Wu

Upload: kolya

Post on 13-Feb-2016

36 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: Socket Programming

Socket Programming

Present: KS Wu

Page 2: Socket Programming

2003/10/6 NTUEECS COBRA LAB 2

Outline

UDP client/server communication Introduction of socket functions How MSN Messenger works

Page 3: Socket Programming

2003/10/6 NTUEECS COBRA LAB 3

Outline

UDP client/server communication Introduction of socket functions How MSN Messenger works

Page 4: Socket Programming

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

Page 5: Socket Programming

2003/10/6 NTUEECS COBRA LAB 5

Outline

UDP client/server communication Introduction of socket functions How MSN Messenger works

Page 6: Socket Programming

2003/10/6 NTUEECS COBRA LAB 6

Socket functions

WSAStartup( )

socket( )bind( )sendto( )recvfrom( )closesocket( )

Page 7: Socket Programming

2003/10/6 NTUEECS COBRA LAB 7

WSAStartup Function

WSADATA wsadata;

WSAStartup(0x101, (LPWSADATA) &wsadata)

version: 1.1

Page 8: Socket Programming

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

Page 9: Socket Programming

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

Page 10: Socket Programming

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

Page 11: Socket Programming

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.

Page 12: Socket Programming

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

Page 13: Socket Programming

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.

Page 14: Socket Programming

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.

Page 15: Socket Programming

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

Page 16: Socket Programming

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.

Page 17: Socket Programming

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.

Page 18: Socket Programming

2003/10/6 NTUEECS COBRA LAB 18

closesocket Function

int closesocket( SOCKET sockfd );

Returns: 0 if OK, negative number on error

Page 19: Socket Programming

2003/10/6 NTUEECS COBRA LAB 19

Parameters

sockfd Descriptor identifying the socket to close.

Page 20: Socket Programming

2003/10/6 NTUEECS COBRA LAB 20

Outline

UDP client/server communication Introduction of socket functions How MSN Messenger works

Page 21: Socket Programming

2003/10/6 NTUEECS COBRA LAB 21

How MSN Messenger works

2 phases Authentication Phase Instant Messaging Phase

Page 22: Socket Programming

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

Page 23: Socket Programming

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

Page 24: Socket Programming

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

Page 25: Socket Programming

2003/10/6 NTUEECS COBRA LAB 25

Server Components

Dispatch serverNotification serverSwitchboard server

Page 26: Socket Programming

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

Page 27: Socket Programming

2003/10/6 NTUEECS COBRA LAB 27

Notification server

authenticate, synchronize user propertiesexchange asynchronous event

notifications

Page 28: Socket Programming

2003/10/6 NTUEECS COBRA LAB 28

Switchboard server

provide instant messaging sessions

Page 29: Socket Programming

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

Page 30: Socket Programming

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

Page 31: Socket Programming

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

Page 32: Socket Programming

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

Page 33: Socket Programming

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

Page 34: Socket Programming

2003/10/6 NTUEECS COBRA LAB 34

Scenario

Client

NS SS

Sending a instant message

Msg

Receiving an Instant Message

Msg

Page 35: Socket Programming

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

Page 36: Socket Programming

Thanks for attention!