Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programmazione Di Rete Con UDP: Interazione Tra Client e Server

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

Programmazione di rete con UDP

UDP: nessuna connessione


! Invio: il mittente indica
esplicitamente indirizzo IP
e porta del ricevente Applicazioni
! Il ricevente deve
esplicitamente estrarre Trasferimento non affidabile di
indirizzo IP e n. di porta Messaggi tra client e server
da pacchetti in arrivo
UDP: pacchetti possono
essere persi o arrivare
fuori ordine

2: Application Layer 1

Interazione tra client e server


Server (running on hostid) Client

create socket, create socket,


port=x, for clientSocket =
incoming request: DatagramSocket()
serverSocket =
DatagramSocket()
Create, address (hostid, port=x,
send datagram request
using clientSocket
read request from
serverSocket

write reply to
serverSocket
read reply from
specifying client
clientSocket
host address,
port umber close
clientSocket

2: Application Layer 2
Classe InetAddress

2: Application Layer 3

Example: Java client (UDP)


import java.io.*;
import java.net.*;

class UDPClient {
public static void main(String args[]) throws Exception
{
Create
input stream BufferedReader inFromUser =
new BufferedReader(new InputStreamReader(System.in));
Create
client socket DatagramSocket clientSocket = new DatagramSocket();
Traduzione
InetAddress IPAddress = InetAddress.getByName("hostname");
Indirizzo
usando DNS byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];

String sentence = inFromUser.readLine();


sendData = sentence.getBytes();
2: Application Layer 4
Example: Java client (UDP), cont.
Crea datagramma
con dati da spedire, DatagramPacket sendPacket =
lungh., ind. IP, porta new DatagramPacket(sendData, sendData.length, IPAddress, 9876);

send datagramma clientSocket.send(sendPacket);


al server
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
Ricevi datagramma
clientSocket.receive(receivePacket);
dal server
String modifiedSentence =
new String(receivePacket.getData());

System.out.println("FROM SERVER:" + modifiedSentence);


clientSocket.close();
}
}

2: Application Layer 5

Example: Java server (UDP)


import java.io.*;
import java.net.*;

class UDPServer {
public static void main(String args[]) throws Exception
{
Crea Socket
su porta 9876 DatagramSocket serverSocket = new DatagramSocket(9876);

byte[] receiveData = new byte[1024];


byte[] sendData = new byte[1024];

while(true)
{
Alloca buffer di
DatagramPacket receivePacket =
Ricezione pacchetto new DatagramPacket(receiveData, receiveData.length);
Ricevi serverSocket.receive(receivePacket);
pacchetto
2: Application Layer 6
Example: Java server (UDP), cont
String sentence = new String(receivePacket.getData());
Estrai ind. IP
InetAddress IPAddress = receivePacket.getAddress();
da pacchetto
ricevuto int port = receivePacket.getPort();

String capitalizedSentence = sentence.toUpperCase();

sendData = capitalizedSentence.getBytes();
Costruisci
DatagramPacket sendPacket =
datagramma new DatagramPacket(sendData, sendData.length, IPAddress,
port);
Scrivi datagram
serverSocket.send(sendPacket);
Su socket }
}
}
Fine ciclo while
Attendi altro datagramma
2: Application Layer 7

2: Application Layer 8

You might also like