inserimento codice socket udp
This commit is contained in:
BIN
USocket/dist/USocket.jar
vendored
BIN
USocket/dist/USocket.jar
vendored
Binary file not shown.
@@ -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
|
||||
|
||||
42
USocket/src/com/mirimatcode/UDatagram.java
Normal file
42
USocket/src/com/mirimatcode/UDatagram.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user