"Cliente" UDP en JAVA

System.out.println("El cliente UDP"); int port = 32123; BufferedReader entradaDesdeUsuario = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket socketCliente = null; try { socketCliente = new DatagramSocket(); } catch (IOException e) { System.out.println("Error al crear el objeto socket cliente"); System.exit ( 0 ); } InetAddress DireccionIP = null; try { DireccionIP = InetAddress.getByName("127.0.0.1"); } catch (IOException e) { System.out.println("Error al recuperar la IP del proceso"); System.exit ( 0 ); } byte [] enviarDatos = new byte[1024]; byte [] recibirDatos = new byte[1024]; int count = 1; while(count <= 1000) { String frase = "move"; enviarDatos = frase.getBytes(); DatagramPacket enviarPaquete = new DatagramPacket(enviarDatos, enviarDatos.length, DireccionIP, port); try { socketCliente.send(enviarPaquete); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Thread.sleep(1); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } count += 1; } DatagramPacket recibirPaquete = new DatagramPacket(recibirDatos, recibirDatos.length); /*try { socketCliente.receive(recibirPaquete); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ String fraseModificada = new String(recibirPaquete.getData()); InetAddress direccion = (recibirPaquete.getAddress()); int puertoUsado = (recibirPaquete.getPort()); System.out.println("DEL SERVIDOR: " + fraseModificada); System.out.println("La direccion IP es:"+ direccion); System.out.println("El puerto es:"+ puertoUsado); socketCliente.close();

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.