ping pong über Sockets
-
2 Threads:
ping: sendet "ping" und wartet auf "pong", sendet dann erneut "ping" usw.
pong: ist die gegenstelle zu ping.
Code:
import java.net.*; import java.io.*; class Ping extends Thread { int count; InetAddress adr; int port; byte[] msg = "ping".getBytes(); DatagramSocket socket; DatagramPacket packet; Ping(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; msg = "ping".getBytes(); socket = new DatagramSocket(); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { try { for(int i=0; i < count;) { socket.send(packet); System.out.println(msg); DatagramPacket pong_packet = new DatagramPacket(msg, msg.length); socket.receive(pong_packet); if ( pong_packet.getData().toString() == "pong" ) { System.out.println(pong_packet.getData()); i++; } } socket.close(); } catch (Exception e){} } } class Pong extends Thread { int count; InetAddress adr; int port; byte[] msg = "pong".getBytes(); DatagramSocket socket; DatagramPacket packet; Pong(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; socket = new DatagramSocket(); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { for(int i=0; i < count;) { try { DatagramPacket ping_packet = new DatagramPacket(msg, msg.length); socket.receive(ping_packet); if ( ping_packet.getData().toString() == "ping" ) { System.out.println(ping_packet.getData()); i++; } msg = "pong".getBytes(); socket.send(packet); System.out.println(msg); } catch(Exception e) {} } socket.close(); } } class pingpong { // ping/pong count adr port public static void main(String args[]) throws Exception { int count = Integer.parseInt(args[1]); int port = Integer.parseInt(args[3]); if ( "ping" == args[0] ) new Ping(args[2], port, count).start(); else if ( "pong" == args[0] ) new Pong(args[2], port, count).start(); } }
Ping und Pong lasse ich jeweils auf dem selben Rechner laufen.
Allerdings terminieren beide Threads ohne jede Ausgabe?Woran könnte das liegen?
Danke
curry-king
-
Hast du mal geschaut ob es Exceptions gibt, die von dem catch abgefangen werden?
-
e.printStackTrace();
bringt nichts.
Noch ne Idee?Danke für alle Antworten!
curry-king
-
Noch eine Idee:
Versuch's mal mit:
if ( "ping".equals(args[0]) ) new Ping(args[2], port, count).start(); else if ( "pong".equals(args[0]) ) new Pong(args[2], port, count).start();
Der ==-Operator prüft in deinem Fall auf Objektidentität und nicht ob die beiden denselben Wert haben.
-
Ich schnalls nicht!
Zuerst starte ich den pong thread, dann den ping thread.
Die Bytearrays wandle ich mit "new String(...)" in Strings um und
vergleiche sie.als ausgabe erhalte ich aber nur: [B@33220
Hat jemand Rat?
Danke
import java.net.*; import java.io.*; class Ping extends Thread { int count; InetAddress adr; int port; byte[] msg = "ping".getBytes(); DatagramSocket socket; DatagramPacket packet; Ping(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; msg = "ping".getBytes(); socket = new DatagramSocket(); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { try { for(int i=0; i < count;) { System.out.println("Hallo from ping"); socket.send(packet); System.out.println(msg); DatagramPacket pong_packet = new DatagramPacket(msg, msg.length); socket.receive(pong_packet); if ( new String(pong_packet.getData()).equals("ping") ) { System.out.println(new String(pong_packet.getData())); i++; } } socket.close(); } catch (Exception e){ e.printStackTrace();} } } class Pong extends Thread { int count; InetAddress adr; int port; byte[] msg = "pong".getBytes(); DatagramSocket socket; DatagramPacket packet; Pong(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; socket = new DatagramSocket(); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { for(int i=0; i < count;) { System.out.println("Hallo from pong"); try { DatagramPacket ping_packet = new DatagramPacket(msg, msg.length); socket.receive(ping_packet); if ( new String(ping_packet.getData()).equals("ping") ) { System.out.println(new String(ping_packet.getData())); i++; } msg = "pong".getBytes(); socket.send(packet); System.out.println(msg); } catch(Exception e) {e.printStackTrace();} } socket.close(); } } class pingpong { // ping/pong count adr port public static void main(String args[]) throws Exception { int count = Integer.parseInt(args[1]); int port = Integer.parseInt(args[3]); Ping ping = new Ping(args[2], port, count); Pong pong = new Pong(args[2], port, count); if ( "ping".equals(args[0]) ) ping.start(); else if ( "pong".equals(args[0]) ) pong.start(); } }
-
msg = "pong".getBytes(); ... System.out.println(msg);
msg ist ein Array, println(msg) gibt dir die Addresse des arrays zurück oder so, du musst das array in ein string umwandeln, bei java ist das nicht so wie bei c/c++. (wo ein char array als string behandelt wird)
machs mal so :
String builder; msg = "pong".getBytes(); builder = new String(msg); System.out.println(builder);
-
Soll "Pong" der Server sein?
Sieht nach nem Client aus...?
-
Änderungen
Pong: socket = new DatagramSocket(port);
main: Objekte nur erzeugen, wenn gebrauchtund ein paar Println sind anders.
import java.net.*; import java.io.*; class Ping extends Thread { int count; InetAddress adr; int port; byte[] msg = "ping".getBytes(); DatagramSocket socket; DatagramPacket packet; Ping(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; msg = "ping".getBytes(); socket = new DatagramSocket(); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { try { for(int i=0; i < count;) { System.out.println("Hallo from ping"); socket.send(packet); System.out.println(" +++ ping send " + new String(msg) + " +++"); DatagramPacket pong_packet = new DatagramPacket(msg, msg.length); socket.receive(pong_packet); if ( new String(pong_packet.getData()).equals("ping") ) { System.out.println(new String(pong_packet.getData())); i++; } } socket.close(); } catch (Exception e){ e.printStackTrace();} } } class Pong extends Thread { int count; InetAddress adr; int port; byte[] msg = "pong".getBytes(); DatagramSocket socket; DatagramPacket packet; Pong(String adr, int port, int count) throws Exception { this.adr = InetAddress.getByName(adr); this.count = count; socket = new DatagramSocket(port); //socket.bind(new InetSocketAddress(port)); packet = new DatagramPacket(msg, msg.length, this.adr, port); } public void run() { for(int i=0; i < count;) { System.out.println("Hallo from pong"); try { DatagramPacket ping_packet = new DatagramPacket(msg, msg.length); socket.receive(ping_packet); System.out.println(" +++ pong data received +++"); if ( new String(ping_packet.getData()).equals("ping") ) { System.out.println(new String(ping_packet.getData())); i++; } msg = "pong".getBytes(); socket.send(packet); System.out.println(msg); } catch(Exception e) {e.printStackTrace();} } socket.close(); } } class pingpong { // ping/pong count adr port public static void main(String args[]) throws Exception { int count = Integer.parseInt(args[1]); int port = Integer.parseInt(args[3]); if ( "ping".equals(args[0]) ) { Ping ping = new Ping(args[2], port, count); ping.start(); } else if ( "pong".equals(args[0]) ) { Pong pong = new Pong(args[2], port, count); pong.start(); } } }