Alexvans

Untitled

Feb 1st, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. package socketsUDP;
  2.  
  3. import java.net.*;
  4. import java.io.*;
  5.  
  6. public class ServidorUDP {
  7.  
  8.     public static void main(String args[]) {
  9.         DatagramSocket socket = null;
  10.         try {
  11.             int puertoServidor = 6789;
  12.  
  13.             socket = new DatagramSocket(puertoServidor);
  14.             // Crea el socket en el puerto acordado
  15.  
  16.             byte[] buffer;
  17.             buffer = new byte[1000];
  18.  
  19.             while (true) {
  20.                 DatagramPacket solicitud;
  21.                 solicitud = new DatagramPacket(buffer, buffer.length);
  22.                 socket.receive(solicitud);
  23.  
  24.                 DatagramPacket respuesta;
  25.                 respuesta = new DatagramPacket(solicitud.getData( ), solicitud.getLength( ),
  26.                         solicitud.getAddress( ), solicitud.getPort( ));
  27.                 socket.send(respuesta);
  28.             }
  29.         } catch (SocketException e) {
  30.             System.out.println("SocketException: " + e.getMessage( ));
  31.         } catch (IOException e) {
  32.             System.out.println("IOException: " + e.getMessage( ));
  33.         } finally {
  34.             if (socket != null) {
  35.                 socket.close( );
  36.             }
  37.         }
  38.     }
  39. }
Add Comment
Please, Sign In to add comment