Introduction À La Programmation Réseau

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.

Introduction à la
Programmation réseau
Professeur adjoint Chaiporn
Jaikaeo, Ph. d. chaiporn.j@ku.ac.th
http://www.cpe.ku.ac.th/~cpj
Computer Engineering Department
Kasetsart University, Bangkok,
Thaïlande

Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.
Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.

Grandes lignes
n Communication to-end
n Communication série directe
n Communication sur Internet
n Applications client-serveur simple

Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.
2
Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.

Communication to-End
n Communication directe
n Les périphériques communicants sont directement connectés
TX TX
RX RX
GND GND

n Communication sur Internet


n La communication est assurée par Internet

Internet

Multilizer PDF Translator Free version - translation is limited to ~ 3 pages per translation.
3
Client-Server Paradigm

Client Server

n Client
n Initiates communication
n Issues command
n Server
n Awaits and respond to commands
n Most common model for Internet applications
4
Our Simple Protocol
command
response
Client Server
Request Issued by
Response by Server
Client
id\r\n Your student ID, followed by \r\n
name\r\n Your name, followed by \r\n

Server responds with "ERROR" when receiving


an unknown command.

5
Hands-on Activity 1: Serial Comm.
n Most basic form of device-to-device
communication
n However, most recent computers do
not come with a serial port
n Use USB-Serial dongle instead

TX TX

RX RX

GND GND
USB- USB-
Serial Serial
Dongle Dongle
6
Python's Serial API
n Opening a serial port
from serial import Serial
ser = Serial("/dev/ttyUSB0")

n Sending data with newline characters


ser.write('Hello\r\n')

n Receiving data (blocking call)


n Wait until new line characters are received and return the whole line
ser.readline()

n Wait until 100 bytes are received


ser.read(100)

7
Internet Comm. - App's Viewpoint
n Two network applications should interact as
if they were directly connected
write read
App App

A B
Internet

8
Internet Comm. – Socket API
n API for developing applications that perform inter-
process communication
n most commonly for communications across a computer
network
n Example functions
n listen – used by server to wait for contact from client
connect – used by client to contact server

n send – used by either client or server to send data
n recv – used by either client or server to receive data
n close – close the connection

9 9
Services Provided by Socket API
n Connection-oriented, stream-like service
n Provides virtual stream-oriented pipe
n Data transfer is reliable
n No loss, in-order arrival

App App
A B
Internet

n Both machines use Transmission Control Protocol


(TCP) to transfer data

10
Services Provided by Socket API
n Connectionless, datagram service
n User must prepare packet of data before sending
n Data transfer is NOT reliable
n Loss possible, out-of-order arrival possible
App App
A B
Internet

n Both machines use User-Datagram Protocol


(UDP) to transfer data
11
Port Addressing
n IP addresses are used to identify hosts (i.e.,
machines) on the Internet
n Port numbers are used to distinguish
different processes running on the same host
Proc4
Proc2
Ports Proc3
Proc1 Proc5

A B
Internet
IP Address 1 IP Address 2

12
Hands-on Activity 2: Internet Comm.
n Implement Activity 1 over the Internet using
stream-oriented service
TCP Socket: Flow in Python
Client Server

bind()
listen()

connect() accept()

send() receive()

recv() send()

close() close()

14
Server: Creating Socket
n Create a socket object and bind it to port
12345 on any available network interface
from socket import * Listen on any
Port 12345
interface

listen_sock = socket()
listen_sock.bind(('0.0.0.0',12345))
listen_sock.listen(1) # max. of 1 client can wait
sock,info = listen_sock.accept()

15
Client: Create Socket
n On another machine, create a client socket
and connect to the server's IP and port

from socket import *

sock = socket()
server = ("<server's",IP>
12345)
sock.connect(server)

16
Finding Out Your IP Address
n Windows – Run ipconfig from a command prompt
n MacOS/Linux/Unix – Run ifconfig from a terminal

17
Server: Check Client's IP and Port
getpeername()
n The method tells us about
client's IP address and port number
n The method returns a tuple (ip-addr, port)

addr,port = sock.getpeername()
print "Client is connected from",addr

18
Transferring Data
n Use send() and recv() methods to send
and receive data, respectively
n E.g., specify the
max number of
n At the server, enter the command bytes to be
received

msg = sock.recv(1024)

The server will block until it receives data

n At the client, enter the command


sock.send('hello')

19
Closing Connection
n Server:
sock.close()
listen_sock.close()

n Client:
sock.close()

20
Assignment: Throw me your name
n I will be running a server on my machine
n For each of you
n Create a socket to connect to my machine
n Then send a line containing
<student id>,<first-last name>,<nickname>,<email>
n Server will close connection immediately

21

You might also like