Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package socketsUDP;
- import java.net.*;
- import java.io.*;
- public class ServidorUDP {
- public static void main(String args[]) {
- DatagramSocket socket = null;
- try {
- int puertoServidor = 6789;
- socket = new DatagramSocket(puertoServidor);
- // Crea el socket en el puerto acordado
- byte[] buffer;
- buffer = new byte[1000];
- while (true) {
- DatagramPacket solicitud;
- solicitud = new DatagramPacket(buffer, buffer.length);
- socket.receive(solicitud);
- DatagramPacket respuesta;
- respuesta = new DatagramPacket(solicitud.getData( ), solicitud.getLength( ),
- solicitud.getAddress( ), solicitud.getPort( ));
- socket.send(respuesta);
- }
- } catch (SocketException e) {
- System.out.println("SocketException: " + e.getMessage( ));
- } catch (IOException e) {
- System.out.println("IOException: " + e.getMessage( ));
- } finally {
- if (socket != null) {
- socket.close( );
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment