UDP Echo Client : UDP Client « Network « Java Tutorial






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class MainClass {

  public static void main(String[] args) throws Exception {
    String hostname = "localhost";
    InetAddress ia = InetAddress.getByName(hostname);
    SenderThread sender = new SenderThread(ia, 1919);
    sender.start();
    Thread receiver = new ReceiverThread(sender.getSocket());
    receiver.start();
  }

}

class SenderThread extends Thread {

  private InetAddress server;

  private DatagramSocket socket;

  private boolean stopped = false;

  private int port;

  public SenderThread(InetAddress address, int port) throws SocketException {
    this.server = address;
    this.port = port;
    this.socket = new DatagramSocket();
    this.socket.connect(server, port);
  }

  public void halt() {
    this.stopped = true;
  }

  public DatagramSocket getSocket() {
    return this.socket;
  }

  public void run() {

    try {
      BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
      while (true) {
        if (stopped)
          return;
        String theLine = userInput.readLine();
        if (theLine.equals("."))
          break;
        byte[] data = theLine.getBytes();
        DatagramPacket output = new DatagramPacket(data, data.length, server, port);
        socket.send(output);
        Thread.yield();
      }
    }
    catch (IOException ex) {
      System.err.println(ex);
    }
  }
}

class ReceiverThread extends Thread {
  DatagramSocket socket;

  private boolean stopped = false;

  public ReceiverThread(DatagramSocket ds) throws SocketException {
    this.socket = ds;
  }

  public void halt() {
    this.stopped = true;
  }

  public void run() {
    byte[] buffer = new byte[65507];
    while (true) {
      if (stopped)
        return;
      DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
      try {
        socket.receive(dp);
        String s = new String(dp.getData(), 0, dp.getLength());
        System.out.println(s);
        Thread.yield();
      } catch (IOException ex) {
        System.err.println(ex);
      }
    }
  }
}








19.17.UDP Client
19.17.1.UDP Echo Client
19.17.2.UDP Discard Client
19.17.3.UDP Time Client
19.17.4.Send a Datagram
19.17.5.Sending a Datagram
19.17.6.Receiving a Datagram
19.17.7.Read and write with DatagramPacket
19.17.8.User Datagram Protocol Programming