/** Running the server watch thread (UDP) */ public void run() { byte[] buffer = new byte[5000]; DatagramPacket packet; try { // Create a socket MulticastSocket socket = new MulticastSocket(port + 1); InetAddress address = InetAddress.getByName("224.0.0.31"); socket.joinGroup(address); while (true) { packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); // Will wait until new packet received - Hence separate thread if (firstRun) { firstRun = false; // Create thread to handle socket send new ClientSendThread(packet.getAddress(), this, client).start(); } ByteArrayInputStream bais = new ByteArrayInputStream(buffer); ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(bais)); Packet pkt = (Packet) ois.readObject(); // Check the packet type - perform action based on this if (pkt instanceof PointPacket) { // Send packet to Canvas canvas.drawPoints((PointPacket) pkt); } if (pkt instanceof ChatPacket) { if (!((ChatPacket) pkt).get_sender().equals("KEEPALIVE")) { chat.drawMessage((ChatPacket) pkt); } } } } catch (Exception e) { System.err.println("Exception"); } }
/** For recv points from the server PUBLIC method! */ public void sendPoints(PointPacket pnt) { canvas.drawPoints(pnt); }