Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Programmazione Di Rete Con UDP: Interazione Tra Client e Server

This document discusses UDP networking programming. It states that UDP uses an unconnected model where the sender explicitly specifies the recipient's IP address and port number, and the recipient must extract this from incoming packets. Packets may be lost or arrive out of order with UDP. It provides examples of Java client and server code for sending and receiving UDP datagrams. The client code creates a socket, sends a datagram to the server specifying the recipient address and port, and receives a reply datagram. The server code creates a socket bound to port 9876, receives datagrams on this port, extracts the sender address and port from packets, uppercase the message, and sends a reply datagram back to the client.

Uploaded by

Moschimoschi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Programmazione Di Rete Con UDP: Interazione Tra Client e Server

This document discusses UDP networking programming. It states that UDP uses an unconnected model where the sender explicitly specifies the recipient's IP address and port number, and the recipient must extract this from incoming packets. Packets may be lost or arrive out of order with UDP. It provides examples of Java client and server code for sending and receiving UDP datagrams. The client code creates a socket, sends a datagram to the server specifying the recipient address and port, and receives a reply datagram. The server code creates a socket bound to port 9876, receives datagrams on this port, extracts the sender address and port from packets, uppercase the message, and sends a reply datagram back to the client.

Uploaded by

Moschimoschi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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