From 5248ae94d118b339a4ef879459f6a017538d4275 Mon Sep 17 00:00:00 2001 From: jancoow Date: Thu, 18 Jun 2015 16:33:22 +0200 Subject: [PATCH] New network thing.. --- control/NetworkHandler.java | 37 +++++++++++++++++++++++++------------ main/Window.java | 4 +++- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/control/NetworkHandler.java b/control/NetworkHandler.java index 2258cd9..c8362c4 100644 --- a/control/NetworkHandler.java +++ b/control/NetworkHandler.java @@ -12,6 +12,7 @@ import control.joystick.JoystickHandler; public class NetworkHandler implements Runnable{ DatagramSocket udp; + DatagramSocket udpsend; String host; int port; @@ -25,6 +26,8 @@ public class NetworkHandler implements Runnable{ ButtonHandler bth; JoystickHandler jth; + InetAddress adr; + public NetworkHandler(String host, int port, ButtonHandler bth, JoystickHandler jth) { this.host = host; @@ -38,8 +41,19 @@ public class NetworkHandler implements Runnable{ send = new byte[1024]; receive = new byte[1024]; - try { - udp = new DatagramSocket(1112); + try { + adr = InetAddress.getByName("192.168.1.5"); + } catch (UnknownHostException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } +// try { +// udp = new DatagramSocket(1112); +// } catch (SocketException e) { +// e.printStackTrace(); +// } + try { + udpsend = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } @@ -64,7 +78,8 @@ public class NetworkHandler implements Runnable{ { send = str.getBytes(); try { - udp.send(new DatagramPacket(send, send.length)); + System.out.println(send); + udpsend.send(new DatagramPacket(send, send.length, adr, port)); } catch (IOException e) { e.printStackTrace(); } @@ -84,22 +99,20 @@ public class NetworkHandler implements Runnable{ { DatagramPacket receivePacket = new DatagramPacket(receive, receive.length); try { - udp.receive(receivePacket); + udpsend.receive(receivePacket); } catch (IOException e) { e.printStackTrace(); } String sentence = new String( receivePacket.getData()); - - if(sentence.length() != 21) - return; - System.out.println("RECEIVED: " + sentence); - + + + sentence = sentence.trim(); String[] controls = sentence.split("\\|"); int[] control = new int[controls.length]; - for(int i=0; i