0% found this document useful (0 votes)
29 views

0-network-programming

Uploaded by

brasileiroyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

0-network-programming

Uploaded by

brasileiroyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Introduction to

Network Programming
Ítalo Cunha

1
Interprocess communication
To establish communication between two processes, both need to cooperate

Server: process that waits for a connection


● Configures a device to receive a connection
● Waits for clients to connect
● Passive endpoint

Client: process that starts a connection


● Needs to “where” the server is waiting for connection
● Active endpoint

2
Network Programming Interfaces
Languages and libraries provide different abstractions for network communication
● POSIX sockets
● AsyncIO
● Message queues and publish/subscribe

Different network technologies depending on the field


● We will focus on the Internet Protocol (IP)

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()

Active opening Internet


Passive opening
connect()
accept()
Internet Communication Communication
send()
Passive opening recv()

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

● IPv4 addresses have 32 bits (150.164.8.15)


● IPv6 addresses have 128 bits (2a03:2880:f111:181:face:b00c:0:25de)
● Port numbers have 16 bits (up to 65535)
○ Port numbers below 2048 are reserved and need superuser privileges to bind

22

You might also like