0-network-programming
0-network-programming
Network Programming
Ítalo Cunha
1
Interprocess communication
To establish communication between two processes, both need to cooperate
2
Network Programming Interfaces
Languages and libraries provide different abstractions for network communication
● POSIX sockets
● AsyncIO
● Message queues and publish/subscribe
3
Connection establishment
Server
Time
4
Connection establishment
Server
Time Initialization
Passive opening
5
Connection establishment
Server
Time Initialization Functions in blue execute on the
socket() local machine and return
Passive opening immediately.
bind()
listen()
Functions in orange
accept() communicate with the remote
device and may take arbitrarily
long to return as they wait for a
response.
6
Connection establishment
Server
Time Initialization Functions in blue execute on the
socket() Client local machine and return
Initialization immediately.
Passive opening
bind()
listen() Active opening Functions in orange
accept() communicate with the remote
device and may take arbitrarily
Internet long to return as they wait for a
response.
7
Connection establishment
Server
Time Initialization Functions in blue execute on the
socket() Client local machine and return
Initialization immediately.
Passive opening
socket()
bind()
listen() Active opening Functions in orange
accept() connect() communicate with the remote
device and may take arbitrarily
Internet long to return as they wait for a
response.
8
Network communication
Server
Time Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Communication
recv()
9
Network communication
Server
Time Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Communication
recv()
Communication
send()
recv()
Internet
10
Network communication
Server
Time Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Communication
recv()
Communication
send()
recv()
send() Internet
Termination
close()
11
Connection termination
Server
Time Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Communication
recv()
Communication
send()
recv()
send() Internet
Termination
close()
Termination
close()
12
Connection termination
Server
Time Initialization Communication only succeeds if
socket() Client both client and server call recv()
Initialization and send() in the correct order!
Passive opening
socket()
bind()
listen() Active opening The sequence of calls to recv()
accept() connect() and send() is defined by the
communication protocol.
Internet
Communication
recv()
Communication
send()
recv()
send() Internet
Termination
close()
Termination
close()
13
Connection termination
Server
Time Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Communication
recv()
Communication
send()
recv()
send() Internet
Termination
close()
Termination
Passive opening close()
accept()
14
Establishing concurrent connections
Server
Problem: Second client cannot
Time establish connection if server is Initialization
socket() Client
not ready and waiting.
Initialization
Passive opening
socket()
Client bind()
listen() Active opening
Initialization
socket() accept() connect()
Active opening
Internet
connect() Communication
recv()
Communication
send()
recv()
send() Internet
Termination
close()
Termination
Passive opening close()
accept()
15
Establishing concurrent connections
Server
Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
16
Establishing concurrent connections
Server
Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Passive opening
accept()
Communication
recv()
Thread
17
Establishing concurrent connections
Server
Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Passive opening
accept()
Communication Communication
send()
recv()
recv()
Thread
18
Establishing concurrent connections
Server
Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Passive opening
accept()
Communication Communication
send()
recv()
recv()
send()
Termination
close()
Thread
19
Establishing concurrent connections
Server
Initialization
socket() Client
Initialization
Passive opening
socket()
bind()
listen() Active opening
accept() connect()
Internet
Passive opening
accept()
Communication Communication
send()
recv()
recv()
send()
Termination
close()
Termination
Thread close()
20
Establishing concurrent connections
Server
Initialization
socket() Client 1
Initialization
Passive opening
socket()
bind()
Client 2 listen() Active opening
Initialization
accept() connect()
socket()
accept() recv()
Communication
Communication send()
send()
recv()
recv() Termination
send() close()
Termination
Termination Thread close()
close()
Termination
close() Thread 21
Connecting to an application
● To connect to a network application, we need two pieces of information
○ The IP address of the device where the application is running
○ The port number assigned to the application we want to connect to
22