Advertisement
simonebortolin

UDP Time Client 01

Oct 11th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.nio.*;
  4. import java.util.Date;
  5.  
  6. public class UDPTimeClient {
  7.     private DatagramSocket socket;
  8.  
  9.     public UDPTimeClient() throws SocketException {
  10.         socket = new DatagramSocket();
  11.         socket.setSoTimeout(2000); // 1000ms = 1s
  12.     }
  13.  
  14.     public void closeSocket() {
  15.         socket.close();
  16.     }
  17.  
  18.     public long getTime(String host, int port) throws UnknownHostException, IOException, SocketTimeoutException {
  19.         ByteBuffer input, output;
  20.         byte[] buffer = new byte[1024];
  21.         DatagramPacket datagram;
  22.         InetAddress address = InetAddress.getByName(host);
  23.         // indirizzo IP del destinatario del datagram
  24.  
  25.         if (socket.isClosed()) {
  26.             throw new IOException();
  27.         } // verifica chiusura socket
  28.         output = ByteBuffer.allocate(2);
  29.         output.putChar('?');
  30.         datagram = new DatagramPacket(output.array(), 2, address, port);
  31.         // costruzione datagram di richiesta
  32.         socket.send(datagram);
  33.         // trasmissione datagram di richiesta
  34.         datagram = new DatagramPacket(buffer, buffer.length);
  35.         // costruzione datagram di risposta
  36.         socket.receive(datagram);
  37.         // attesa ricezione datagram di richiesta (tempo massimo di attesa: 1s)
  38.         // verifica indirizzo/porta provenienza datagram di risposta
  39.         if (datagram.getAddress().equals(address) && datagram.getPort() == port) {
  40.             // estrazione di 1 valore long dal datagram di risposta
  41.             input = ByteBuffer.wrap(datagram.getData());
  42.             return input.getLong();
  43.         } else {
  44.             throw new SocketTimeoutException();
  45.         }
  46.     }
  47.  
  48.     public static void main(String args[]) {
  49.         String IP_address = "192.168.134.13";
  50.         int UDP_port = 12345;
  51.         long timestamp;
  52.         UDPTimeClient udp_request;
  53.  
  54.  
  55.         System.out.println("INIZIO");
  56.  
  57.         try {
  58.             udp_request = new UDPTimeClient();
  59.             timestamp = udp_request.getTime(IP_address, UDP_port);
  60.             Date date = new Date(timestamp * 1000);
  61.             System.out.println("Risposta: " + date + "(" + timestamp + ")");
  62.             udp_request.closeSocket();
  63.         } catch (SocketException exception) {
  64.             System.err.println("Errore creazione socket!");
  65.         } catch (UnknownHostException exception) {
  66.             System.err.println("Indirizzo IP errato!");
  67.         } catch (SocketTimeoutException exception) {
  68.             System.err.println("Nessuna risposta dal server!");
  69.         } catch (IOException exception) {
  70.             System.err.println("Errore generico di comunicazione!");
  71.         }
  72.         //main(null);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement