268x Filetype PDF File size 0.17 MB Source: bkict-ocw.knu.ac.kr
Socket programming
Chapter 2-2 goal: learn how to build client/server applications that
Socket Programming communicate using sockets
socket: door between application process and end-
end-transport protocol
application socket application controlled by
process process app developer
Computer transport transport
Networking: A Top network network controlled
Down Approach link Internet link by OS
physical physical
7th Edition, Global Edition
Jim Kurose, Keith Ross
Pearson
April 2016
Socket Programming 2-1 Application Layer 2-2
API (Application Programming Interface) Socket API
API: socket interface allows programmers to write application
Provides a standard set of functions that can be called by programs easily without worrying about the underlying
applications network details
an application process can send and receive messages
Various APIs to/from another application process through this interface
socket API (Berkeley Unix) application only sees local socket
Winsock (MS window) etc. sockets are explicitly created, used, released by
applications
socket API transport services
connection-oriented service: stream-oriented
connectionless service: datagram
Socket Programming 2-3 Socket Programming 2-4
Client & server differences Socket calls for connection-oriented mode
server Server does passive open
Server socketcreates a socket to listen for connection
specifies well-known port # when creating socket: socket() requests
21(ftp) , 23(telnet), 25(SMTP), 80(HTTP) Server specifies type: TCP (stream)
may have multiple IP addresses (net interfaces) bind()
waits passively for client requests listen()
Client
client accept() socket()
assigned ephemeral port # Blocks Connect
initiates communications with server negotiation connect()
needs to know server’s IP address & port # Data
server learns client’s IP address & port # recv() send()
send() Data recv()
Socket Programming 2-5 close() close() Socket Programming 2-6
Socket calls for connection-oriented mode Socket calls for connection-oriented mode
Server does passive open Server does passive open
Server bindassigns local address & port # to the socket Server listenindicates to TCP readiness to receive
socket() Can use wildcard (‘’) IP address socket() connection requests for the socket
Parameter specifies max number of requests that may
bind() bind() be queued while waiting for server to accept them
listen() Client listen() Client
accept() socket() accept() socket()
Blocks Connect Blocks Connect
negotiation connect() negotiation connect()
recv() Data send() recv() Data send()
send() Data recv() send() Data recv()
close() close() Socket Programming 2-7 close() close() Socket Programming 2-8
Socket calls for connection-oriented mode Socket calls for connection-oriented mode
Server does passive open Client does active open
Server Server calls accept to accept incoming requests Server socketcreates socket to connect to server
socket() acceptblocks if queue is empty socket() Client specifies type: TCP (stream)
bind() bind()
listen() Client listen() Client
accept() socket() accept() socket()
Blocks Connect Blocks Connect
negotiation connect() negotiation connect()
recv() Data send() recv() Data send()
send() Data recv() send() Data recv()
close() close() Socket Programming 2-9 close() close() Socket Programming 2-10
Socket calls for connection-oriented mode Socket calls for connection-oriented mode
Client does active open acceptwakes with incoming connection request
Server connectestablishes a connection on the local socket Server acceptcreates a new connection socket
socket() to the specified remote address and port # socket() acceptgets client address (IP addr & port #)
Client & server use new socket for data transfer
bind() bind() Original socket continues to listen for new requests
listen() Client listen()
Client
accept() socket() accept() socket()
Blocks Connect Note: connect Blocks Connect
negotiation connect() initiates TCP three-way negotiation connect()
handshake
recv() Data send() recv() Data send()
send() Data recv() send() Data recv()
close() close() Socket Programming 2-11 close() close() Socket Programming 2-12
Socket calls for connection-oriented mode Socket calls for connection-oriented mode
Data Transfer Data Transfer
Server Client or server call send to transmit data into a Server Client or server call recv to receive data from a
socket() connection socket socket() connection socket
recv specifies: pointer to a buffer; amount of data
bind() bind()
listen() listen()
Client Client
accept() socket() accept() socket()
Blocks Connect Blocks Connect Note: send and recv
negotiation connect() negotiation connect() can be called multiple
times to transfer byte
Data send() Data send() streams in both
recv() recv() directions
send() Data recv() send() Data recv()
close() close() Socket Programming 2-13 close() close() Socket Programming 2-14
Socket calls for connection-oriented mode Socket calls for connectionless mode
Connection Termination Server started
Server Client or server call close when socket is no longer Server socketcreates socket of type UDP (datagram)
socket() needed bindassigns local IP address & port # to the socket;
socket() Can use wildcard IP address
bind()
listen() bind()
Client Client
accept() socket() recvfrom() socket()
Blocks Connect Note: close initiates Data
negotiation connect() TCP graceful close Blocks until server sendto()
sequence receives data from
Data client
recv() send() sendto() Data
send() Data recvfrom()
recv()
close() close() close()
close() Socket Programming 2-15 Socket Programming 2-16
no reviews yet
Please Login to review.