ESERCITAZIONE
-
Upload
mckenzie-may -
Category
Documents
-
view
20 -
download
1
description
Transcript of ESERCITAZIONE
![Page 1: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/1.jpg)
Application Layer 2-1
ESERCITAZIONE
SOCKET PROGRAMMING
![Page 2: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/2.jpg)
Application Layer 2-2
Socket programming
goal: learn how to build client/server applications that communicate using sockets
socket: door between application process and end-end-transport protocol
Internet
controlledby OS
controlled byapp developer
transport
application
physical
link
network
process
transport
application
physical
link
network
processsocket
![Page 3: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/3.jpg)
Application Layer 2-3
Socket programming
Two socket types for two transport services: UDP: unreliable datagram TCP: reliable, byte stream-oriented
Application Example:1. Client reads a line of characters (data)
from its keyboard and sends the data to the server.
2. The server receives the data and converts characters to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays the line on its screen.
![Page 4: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/4.jpg)
Application Layer 2-4
Socket programming with UDPUDP: no “connection” between client &
server no handshaking before sending data sender explicitly attaches IP destination
address and port # to each packet rcvr extracts sender IP address and port# from
received packet
UDP: transmitted data may be lost or received out-of-order
Application viewpoint: UDP provides unreliable transfer of groups of
bytes (“datagrams”) between client and server
![Page 5: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/5.jpg)
Client/server socket interaction: UDP
closeclientSocket
read datagram fromclientSocket
create socket:clientSocket =socket(AF_INET,SOCK_DGRAM)
Create datagram with server IP andport=x; send datagram viaclientSocket
create socket, port= x:serverSocket =socket(AF_INET,SOCK_DGRAM)
read datagram fromserverSocket
write reply toserverSocketspecifying client address,port number
Application 2-5
server (running on serverIP) client
![Page 6: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/6.jpg)
Application Layer 2-6
Example app: UDP client
from socket import *
serverName = ‘hostname’
serverPort = 12000
clientSocket = socket(socket.AF_INET,
socket.SOCK_DGRAM)
message = raw_input(’Input lowercase sentence:’)
clientSocket.sendto(message,(serverName, serverPort))
modifiedMessage, serverAddress =
clientSocket.recvfrom(2048)
print modifiedMessage
clientSocket.close()
Python UDPClientinclude Python’s socket library
create UDP socket for server
get user keyboardinput
Attach server name, port to message; send into socket
print out received string and close socket
read reply characters from
socket into string
![Page 7: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/7.jpg)
Application Layer 2-7
Example app: UDP server
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', serverPort))
print “The server is ready to receive”
while 1:
message, clientAddress = serverSocket.recvfrom(2048)
modifiedMessage = message.upper()
serverSocket.sendto(modifiedMessage, clientAddress)
Python UDPServer
create UDP socket
bind socket to local port number 12000
loop forever
Read from UDP socket into message, getting client’s address (client IP and port)
send upper case string back to this client
![Page 8: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/8.jpg)
Application Layer 2-8
Socket programming with TCPclient must contact server server process must first
be running server must have created
socket (door) that welcomes client’s contact
client contacts server by: Creating TCP socket,
specifying 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 that particular client allows server to talk
with multiple clients source port numbers
used to distinguish clients (more in Chap 3)
TCP provides reliable, in-orderbyte-stream transfer (“pipe”) between client and server
application viewpoint:
![Page 9: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/9.jpg)
Application Layer 2-9
Client/server socket interaction: TCP
wait for incomingconnection requestconnectionSocket =serverSocket.accept()
create socket,port=x, for incoming request:serverSocket = socket()
create socket,connect to hostid, port=xclientSocket = socket()
server (running on hostid) client
send request usingclientSocketread request from
connectionSocket
write reply toconnectionSocket
TCP connection setup
closeconnectionSocket
read reply fromclientSocket
closeclientSocket
![Page 10: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/10.jpg)
Application Layer 2-10
Example app: TCP client
from socket import *
serverName = ’servername’
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
sentence = raw_input(‘Input lowercase sentence:’)
clientSocket.send(sentence)
modifiedSentence = clientSocket.recv(1024)
print ‘From Server:’, modifiedSentence
clientSocket.close()
Python TCPClient
create TCP socket for server, remote port 12000
No need to attach server name, port
![Page 11: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/11.jpg)
Application Layer 2-11
Example app: TCP server
from socket import *
serverPort = 12000
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind((‘’,serverPort))
serverSocket.listen(1)
print ‘The server is ready to receive’
while 1:
connectionSocket, addr = serverSocket.accept()
sentence = connectionSocket.recv(1024)
capitalizedSentence = sentence.upper()
connectionSocket.send(capitalizedSentence)
connectionSocket.close()
Python TCPServer
create TCP welcoming
socket
server begins listening for incoming TCP requests
loop forever
server waits on accept()for incoming requests, new socket created on return
read bytes from socket (but not address as in UDP)
close connection to this client (but not welcoming socket)
![Page 12: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/12.jpg)
Application Layer 2-12
![Page 13: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/13.jpg)
Esercitazione 2
Application Layer 2-13
![Page 14: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/14.jpg)
Esercitazione 2 sol
Application Layer 2-14
![Page 15: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/15.jpg)
Esercitazione 3
Application Layer 2-15
![Page 16: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/16.jpg)
Esercitazione 3 sol.
Application Layer 2-16
![Page 17: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/17.jpg)
Application Layer 2-17
![Page 18: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/18.jpg)
Application Layer 2-18
![Page 19: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/19.jpg)
Application Layer 2-19
![Page 20: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/20.jpg)
2-20
![Page 21: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/21.jpg)
Application Layer 2-21
![Page 22: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/22.jpg)
Application Layer 2-22
![Page 23: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/23.jpg)
Application Layer 2-23
![Page 24: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/24.jpg)
Application Layer 2-24
![Page 25: ESERCITAZIONE](https://reader030.fdocuments.us/reader030/viewer/2022013011/568131d1550346895d983ad1/html5/thumbnails/25.jpg)
2-25