@Override public void run() { try { SocketUtil._sendMessage(msg); } catch (Exception e) { } }
@Override public void run() { PortMapping desiredMapping = new PortMapping(8888, SocketUtil.getLocalIP(), PortMapping.Protocol.UDP, "My Port Mapping"); UpnpService upnpService = new UpnpServiceImpl(new PortMappingListener(desiredMapping)); upnpService.getControlPoint().search(); boolean disconnected = false; Response r = null; while (!disconnected) { try { r = SocketUtil._waitForResponse(); } catch (IOException e) { e.printStackTrace(); } InetAddress ip = r.ip; int socket_index = 0; boolean found = false; System.out.println("Message: "); System.out.println(Arrays.toString(r.data)); System.out.println("Existing IPs: "); System.out.println(SocketUtil.otherIPs.toString()); for (; socket_index < SocketUtil.otherIPs.size(); socket_index++) { if (SocketUtil.otherIPs.get(socket_index).equals(ip)) { found = true; break; } } if (found) { SocketUtil.fireIncomingMessageEvent(socket_index, r.data); } else { // We don't have this ip yet, probably a new player if (r.data[0].equals("connect")) { // brand new player, add their ip and send them the list // of other players try { String other_ips_string = Arrays.toString(SocketUtil.otherIPs.toArray()); other_ips_string.replaceAll("\\[\\]", ""); System.out.println("Sending other ip string: " + other_ips_string); SocketUtil._sendMessage(other_ips_string, r.ip); } catch (IOException e) { e.printStackTrace(); } SocketUtil.otherIPs.add(r.ip); SocketUtil.firePlayerJoinedEvent(); } else if (r.data[0].equals("join")) { // the connecting player has already received a player list // from someone else, just need to add their ip to our list SocketUtil.otherIPs.add(r.ip); SocketUtil.firePlayerJoinedEvent(); } } } }
@Override public void run() { try { // add the 'server' player's ip to the list SocketUtil.otherIPs.add(InetAddress.getByName(ip)); } catch (UnknownHostException e1) { e1.printStackTrace(); } // Send an 'connect' message to the ip we're opening a connection to // This will cause the receiving client to respond with a list of // IPs for any other clients (this is probably hilariously insecure) try { SocketUtil._sendMessage("connect", SocketUtil.otherIPs.get(0)); } catch (IOException e) { e.printStackTrace(); } Response r = null; try { r = SocketUtil._waitForResponse(); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < r.data.length; i++) { System.out.println("other player: " + r.data[i]); if (r.data[i].length() == 0) continue; // Also send a join message to all other clients so that they // will have this client's IP try { SocketUtil._sendMessage("join", InetAddress.getByName(r.data[i])); SocketUtil.otherIPs.add(InetAddress.getByName(r.data[i])); } catch (IOException e) { e.printStackTrace(); } // Add a player for each client SocketUtil.firePlayerJoinedEvent(); } // start listening for incoming messages new Thread(new ListenForIncomingTask()).start(); }