Socket Programming MP 1 Presented by Wally Socket
Socket Programming (MP 1 -) Presented by Wally
Socket programming Goal: learn how to build client/server application that communicate using sockets Socket API r introduced in BSD 4. 1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm r two types of transport service via socket API: v unreliable datagram v reliable, byte stream-oriented socket a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process 2: Application Layer 2
Socket-programming using TCP Socket: a door between application process and end-endtransport protocol (UCP or TCP) TCP service: reliable transfer of bytes from one process to another controlled by application developer controlled by operating system process socket TCP with buffers, variables host or server internet socket TCP with buffers, variables controlled by application developer controlled by operating system host or server 2: Application Layer 3
Socket programming: Terminology Term IP(v 4) Port Socket Format x. x N<65535 [memory/ resource] Example 192. 168. 0. 1 80 s=socket(…) Analogy-phone Phone number Extension # Physical phone
Client/server socket interaction: TCP Server (stand-by, waiting for requests) Create socket (Claim resources/ available phone) Bind port Client (initiate the request) Create socket (Claim resources/ available phone) - (Claim ID on this machine/ get a phone extension No. ) (Don’t care about public port/ phone #) Listen/ accept Connect (Wait for connections/ Wait for phone call) (connect to server/ call others) Send/ receive (Communication/ Chat on phone) Close socket (End communication/ Hang up the phone) Red word: wait for the other side
Client/server socket interaction: TCP Server 1 (stand-by, waiting for requests) Create socket (Claim resources/ available phone) 2 Client (initiate the request) 4 Create socket (Claim resources/ available phone) Bind port (Claim ID on this machine/ get a phone extension No. ) - (Don’t care about public port/ phone #) 3 Listen/ accept 5 Connect 6 Send/ receive Close socket 7/8 (Wait for connections/ Wait for phone call) 7/8 (Communication/ Chat on phone) (End communication/ Hang up the phone) (connect to server/ call others) (Communication/ Chat on phone) Close socket (End communication/ Hang up the phone) Red word: wait for the other side
Socket programming with TCP Client must contact server r server process must first be running r server must have created socket (door) that welcomes client’s contact Client contacts server by: r creating client-local TCP socket r specifying IP address, port number of server process r When client creates socket: client TCP establishes connection to server TCP r When contacted by client, server TCP creates new socket for server process to communicate with client v allows server to talk with multiple clients v source port numbers used to distinguish clients (more in Chap 3) application viewpoint TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server 2: Application Layer 7
Client/server socket interaction: TCP Server (stand-by, waiting for requests) Create socket (Claim resources/ available phone) Client (initiate the request) Create socket (Claim resources/ available phone) Bind port (Claim ID on this machine/ get a phone extension No. ) Listen/ accept Parent Fork/ New thread Connect (connect to server/ call others) Child Send/ receive (Communication/ Chat on phone) Close socket (End communication/ Hang up the phone)
Multiplexing/demultiplexing Multiplexing at send host: gathering data from multiple sockets, enveloping data with header (later used for demultiplexing) Demultiplexing at rcv host: delivering received segments to correct socket = socket application = process P 3 P 1 application transport network link physical host 1 P 2 P 4 application transport network link physical host 2 host 3 Transport Layer 3 -9
How demultiplexing works r host receives IP datagrams each datagram has source IP address, destination IP address v each datagram carries 1 transportlayer segment v each segment has source, destination port number r host uses IP addresses & port numbers to direct segment to appropriate socket v Analogous to airport shuttles Shuttles MUX passengers and take them to downtown -- De. MUX at different locations 32 bits source port # dest port # other header fields application data (message) TCP/UDP segment format Transport Layer 310
Connectionless demultiplexing r Create sockets with port numbers: Datagram. Socket my. Socket 1 = new Datagram. Socket(99111); Datagram. Socket my. Socket 2 = new Datagram. Socket(99222); r UDP socket identified by two- tuple: (dest IP address, dest port number) r When host receives UDP segment: v v checks destination port number in segment directs UDP segment to socket with that port number r IP datagrams with different source IP addresses and/or source port numbers directed to same socket Transport Layer 311
Connectionless demux (cont) Datagram. Socket server. Socket = new Datagram. Socket(6428); P 2 SP: 6428 DP: 9157 SP: 6428 DP: 5775 SP: 9157 client IP: A P 1 P 3 DP: 6428 server IP: C SP: 5775 DP: 6428 Client IP: B SP provides “return address” Transport Layer 312
Connection-oriented demux r TCP socket identified by 4 - tuple: v v source IP address source port number dest IP address dest port number r recv host uses all four values to direct segment to appropriate socket r Server host may support many simultaneous TCP sockets: v each socket identified by its own 4 -tuple r Web servers have different sockets for each connecting client v non-persistent HTTP will have different socket for each request Transport Layer 313
Connection-oriented demux (cont) = socket P 1 = process P 4 P 5 P 2 P 6 P 1 P 3 SP: 5775 DP: 80 S-IP: B D-IP: C client IP: A SP: 9157 DP: 80 S-IP: A D-IP: C SP: 9157 server IP: C DP: 80 S-IP: B D-IP: C Client IP: B Transport Layer 314
Connection-oriented demux: Threaded Web Server = socket P 1 = process P 2 P 4 P 1 P 3 SP: 5775 DP: 80 S-IP: B D-IP: C client IP: A SP: 9157 DP: 80 S-IP: A D-IP: C SP: 9157 server IP: C DP: 80 S-IP: B D-IP: C Client IP: B Transport Layer 315
- Slides: 15