CMPT 880: Internet Architectures and Protocols

Download Report

Transcript CMPT 880: Internet Architectures and Protocols

School of Computing Science
Simon Fraser University
CMPT 771/471: Internet Architecture and Protocols
Socket Programming
Instructor: Dr. Mohamed Hefeeda
1
Network (Socket) Programming
 Process sends/receives
messages to/from its socket
 Socket analogous to door
 sending process shoves
message out door
 sending process relies
on transport
infrastructure on other
side of door which brings
message to socket at
receiving process
host or
server
host or
server
process
controlled by
app developer
process
socket
socket
TCP with
buffers,
variables
Internet
TCP with
buffers,
variables
controlled
by OS
 Socket is the interface (API) between application and transport
layer
2
Addressing Processes
 For a process to receive
messages, it must have an
identifier
 A host has a unique32-bit
IP address
 Q: does the IP address of
the host on which the
process runs suffice for
identifying the process?
 A: No, many processes can
be running on same host 
 We use ports
 Process is identified by:
 IP address,
 Transport protocol, and
 Port number
 Example port numbers:
 HTTP server: 80 (TCP)
 Mail server: 25 (TCP)
3
Socket Programming
 Socket API
 introduced in BSD 4.1 UNIX, 1981
 explicitly created, used, released by apps
 client/server paradigm
 provides two services
• reliable, byte stream-oriented
• unreliable datagram
4
Socket Programming using TCP
 TCP service: reliable transfer of bytes from one
process to another
 virtual pipe between sender and receiver
controlled by
application
developer
controlled by
operating
system
process
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
5
Socket Programming using TCP
Server (running on hostid)
Client
create socket,
port=x, for
incoming request:
welcomeSocket =
ServerSocket()
TCP
wait for incoming
connection request connection
connectionSocket =
welcomeSocket.accept()
read request from
connectionSocket
write reply to
connectionSocket
close
connectionSocket
setup
create socket,
connect to hostid, port=x
clientSocket =
Socket()
send request using
clientSocket
read reply from
clientSocket
close
clientSocket
6
Socket Programming using TCP
 Server process must first be running, and
 creates a socket (door) that welcomes client’s contact,
then wait
 Client contacts server by creating local TCP socket using IP
address, port number of server process
 When client creates socket
 client TCP establishes connection to server TCP
 When contacted by client
 server TCP creates new socket for server process to
communicate with client
• allows server to talk with multiple clients
• source port numbers and IPs used to distinguish clients
7
Socket programming using UDP
 UDP Service: unreliable transfer of groups of bytes
(datagrams) between client and server
 no connection between client and server
 no handshaking
 sender explicitly attaches IP address and port of
destination to each packet
 server must extract IP address, port of sender from
received packet
 transmitted data may be received out of order, or lost
8
Socket Programming using UDP
Server (running on hostid)
create socket,
port=x, for
incoming request:
serverSocket =
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port number
Client
create socket,
clientSocket =
DatagramSocket()
Create datagram (hostid,port=x,data)
send datagram request
using clientSocket
read reply from
clientSocket
close
clientSocket
9