UDP Echo Client : UDP Client « Network « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Network » UDP Client 
19.17.1.UDP Echo Client
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[] argsthrows 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 portthrows 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 dsthrows 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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.