inserimento codice socket udp

This commit is contained in:
2023-02-03 12:21:40 +01:00
parent 573bec9464
commit e02d5b50a9
41 changed files with 4217 additions and 19 deletions

Binary file not shown.

View File

@@ -1,2 +1,2 @@
compile.on.save=true
user.properties.file=/home/boss/.netbeans/16/build.properties
user.properties.file=/home/docente/.netbeans/16/build.properties

View File

@@ -0,0 +1,42 @@
package com.mirimatcode;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class UDatagram {
static final int dim_buffer = 1024;
public static String listenString(int porta) throws SocketException, IOException {
DatagramSocket server = new DatagramSocket(porta);
byte[] buffer = new byte[dim_buffer];
DatagramPacket pacchetto = new DatagramPacket(buffer, dim_buffer);
System.out.println("Datagram Server in ascolto");
server.receive(pacchetto);
System.out.println("Ho ricevuto un messaggio da: " + pacchetto.getAddress().getHostAddress());
String messaggio = new String(pacchetto.getData());
server.close();
return messaggio;
}
public static void writeString(String host, int porta, String messaggio) throws IOException {
InetAddress destinatario = InetAddress.getByName(host);
DatagramSocket client = new DatagramSocket();
byte[] flusso_messaggio = messaggio.getBytes();
DatagramPacket pacchetto = new DatagramPacket(flusso_messaggio, flusso_messaggio.length, destinatario, porta);
client.send(pacchetto);
client.close();
}
}

View File

@@ -97,6 +97,7 @@ public class USocket {
* @throws UnknownHostException
* @throws IOException
*/
public static String writeAndListenString(String ip, int porta, String messaggio) throws UnknownHostException, IOException {
Socket client = new Socket(ip, porta);