Transcript 14_sockets

Inter-Process Communication
i206
Fall 2010
John Chuang
Some slides adapted from Coulouris, Dollimore and Kindberg; Calvert and Donahoo
C/S, P2P
Caching
IPC and
Sockets
Distributed
Systems
Security
Cryptography
Network
Confidentiality
Integrity
Authentication
…
Standards
& Protocols
sockets
Inter-process
Communication
Methodologies/
Tools
TCP/IP,
RSA, …
Principles
UML, CRC
I/O
Operating
System
Process
Context switch
Process vs. Thread
Locks and deadlocks
Memory
hierarchy
Memory
Register, Cache
Main Memory,
Secondary Storage
ALUs, Registers,
Compiler/
Program Counter,
Instruction Register Interpreter
Program
Assembly
Instructions
Op-code, operands
Instruction set arch
Circuits
Lossless v. lossy
Info entropy &
Huffman code
Decimal,
Hexadecimal,
Binary
Data
compression
Data
Data
Representation
John Chuang
Gates
Adders, decoders,
Memory latches,
ALUs, etc.
Boolean
Logic
AND, OR, NOT,
XOR, NAND, NOR,
etc.
Number
Systems
Binary
Numbers
Bits & Bytes
Design
Formal models
Finite automata
regex
Machine
Instructions
CPU
Data
storage
Numbers, text,
audio, video,
image, …
Application
Algorithms
Analysis
Big-O
Data Structures
Searching, sorting,
Encryption, etc.
Stacks, queues,
maps, trees,
graphs, …
Truth table
Venn Diagram
DeMorgan’s Law
2
Inter-Process Communication
 Mechanism for processes to communicate
and to synchronize their actions
- Processes may reside on same or different
machines
John Chuang
Source: Silberschatz, Galvin, Gagne
process p
process q
send m
receiv e
Communic ation c hannel
Outgoing mess age buffer
Incoming mes sage buffer
Source: Coulouris, Dollimore and Kindberg
3
IPC
process p
process q
send m
receiv e
Communic ation c hannel
Outgoing mess age buffer
Incoming mes sage buffer
Source: Coulouris, Dollimore and Kindberg
 IPC facility provides two operations:
- send(message)
- receive(message)
 If P and Q wish to communicate, they need to:
- establish a communication link between them
- exchange messages via send/receive
John Chuang
4
Synchronization
 Processes may want to synchronize their actions
- e.g., process A may want to wait until it receives a
message from process B
 Message passing may be either blocking or nonblocking:
- Blocking is considered synchronous
- Non-blocking is considered asynchronous
 send and receive primitives may be either
blocking or non-blocking
John Chuang
5
How do we Support IPC?
 Socket API (application
programming interface)
- allows the abstraction of
the underlying network
Network
Cloud
Client process
John Chuang
Server process
6
Sockets
 A socket is one endpoint of a two-way
communication link between two programs
running on the network
 A socket (endpoint) is identified by an Internet
address and a port number
socket
any port
agreed port
socket
message
client
server
other ports
Internet address = 138.37.94.248
John Chuang
Internet address = 138.37.88.249
Source: Coulouris, Dollimore and Kindberg
7
Sockets
 We can support multiple communicating processes on a single
machine
 We can have multiple connections between two machines
- Every network connection can be uniquely identified by its two
endpoints
 Since clients initiate connections with server, the server address and
port must be known to clients in advance
socket
any port
agreed port
socket
message
client
server
other ports
Internet address = 138.37.94.248
John Chuang
Internet address = 138.37.88.249
Source: Coulouris, Dollimore and Kindberg
8
Ports
 The port numbers are 16 bits long and are
divided into three ranges:
- Well known ports (0-1023)
- Registered ports (1024-49151)
- Dynamic and/or private ports (49152 – 65535)
 Some “well known ports”
- ftp (21); ssh (22); telnet (23); smtp (25); finger (79);
http (80)
- assigned by Internet Assigned Numbers Authority
(www.iana.org/numbers.html)
John Chuang
9
Two Common Socket Types
 Datagram sockets transfer messages of
variable size in either direction. Supported in
Internet domain by UDP protocol.
 Stream sockets provide reliable, duplex,
sequenced data streams. Supported in
Internet domain by the TCP protocol.
John Chuang
10
TCP Socket Operation
EchoServer
host = ‘ ‘
port = 50000
backlog = 1 # max number of queued connections
size = 1024 # max amount of data to be received
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
(waits for next request)
EchoClient
host = ‘localhost’
port = 50000
size = 1024 # max amount of data to be received
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, world')
data = client.recv(size)
if data:
client.send(data)
client.close()
John Chuang
data = s.recv(size)
print 'Received:', data
s.close()
11
TCP Socket Operation
EchoServer
host = ‘ ‘
port = 50000
backlog = 1 # max number of queued connections
size = 1024 # max amount of data to be received
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,port))
s.listen(backlog)
while 1:
client, address = s.accept()
(waits for next request)
EchoClient
host = ‘localhost’
port = 50000
size = 1024 # max amount of data to be received
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send('Hello, world')
data = client.recv(size)
if data:
client.send(data)
client.close()
John Chuang
data = s.recv(size)
print 'Received:', data
s.close()
12
Assignment 6 Part 1:
Write your own Browser
 HTTP uses TCP (reliable streams)
 Use simple client as starting point:
- Example in Section 18.2.2 of:
- http://docs.python.org/library/socket.html
- Construct and send HTTP request message
- Receive response message from server
John Chuang
13
HTTP Request Message
Format
 HTTP specification (RFC 2616):
http://www.ietf.org/rfc/rfc2616.txt
Request line
General header
Request header
<method><resource identifier><HTTP version><CRLF>
[<Header>:<value>] <CRLF>
…
Entity header
[<Header>:<value>] <CRLF>
Blank line
<CRLF>
Entity body
[Message body]
John Chuang
Note:
• <CRLF> = carriage-return-line-feed = “\r\n” in Python
• optional fields in []
14
HTTP Request: Example
 To request
http://www.ischool.berkeley.edu/index.html
Request line
GET /index.html HTTP/1.1\r\n
Request header
Host: www.ischool.berkeley.edu\r\n
Blank line
\r\n
John Chuang
15
Next Step
 Distributed systems
- Requirements and architectures
 Inter-process communications
- Socket API and network programming
 Networking and Internetworking
- Architecture and design
- Protocols and standards
John Chuang
16