예제 #1
0
  public void run() {

    boolean bContinue = true;

    while (bContinue) {
      try {
        byte[] buf = new byte[256];

        DatagramPacket packet = new DatagramPacket(buf, buf.length);

        /** wait here for incoming data */
        socket.receive(packet);

        /** received data */
        InetAddress address = packet.getAddress();
        int port = packet.getPort();
        byte[] data = packet.getData();

        /** deserialize the data into a String object (what we expect) */
        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data));
        String sData = (String) in.readObject();
        in.close();

        String sText = ui.getTextIncoming().getText();
        sText += address.getHostAddress() + ":" + port + "  " + sData + "\n";
        ui.getTextIncoming().setText(sText);

      } catch (IOException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
  }
예제 #2
0
 public DatagramServer(String name, DatagramUI d) throws IOException {
   super(name);
   ui = d;
   socket = new DatagramSocket(ui.getPort());
 }