Package com.peterhi.net

Examples of com.peterhi.net.Local



public final class ServerApp {

  public static void main(String[] args) throws Exception {
    final Local local = new Local(22222);
    local.getSocket().setSoTimeout(10);
    local.addListener(new Listener() {
      @Override
      public void received(Event event) {
        SocketAddress address = event.getAddress();
        byte[] data = event.getData(true);
        System.out.println(MessageFormat.format("Received {0} bytes of data from {1}.", data.length, address));
      }     
    });
   
    Runnable worker = new Runnable() {
      @Override
      public void run() {
        try {
          while (local.getSocket().isBound()) {
            DatagramPacket packet = local.receive();
           
            if (packet != null) {
              local.process(packet);
            }
           
            local.dispatch();
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
View Full Code Here



public final class ClientApp {

  public static void main(String[] args) throws Exception {
    final Local local = new Local();
    local.getSocket().setSoTimeout(10);
    local.addListener(new Listener() {
      @Override
      public void received(Event event) {
      }
    });
   
    Runnable worker = new Runnable() {
      @Override
      public void run() {
        try {
          while (local.getSocket().isBound()) {
            DatagramPacket packet = local.receive();
           
            if (packet != null) {
              local.process(packet);
            }
           
            local.dispatch();
          }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    };
   
    Thread thread = new Thread(worker);
    thread.start();
   
    InetAddress host = InetAddress.getLocalHost();
//    InetAddress host = InetAddress.getByName("www.peterhi.com");
    int port = 22222;
    SocketAddress address = new InetSocketAddress(host, port);
   
//    while (true) {
      int length = 10000;
      byte[] buffer = new byte[length];
      buffer[length - 1] = 1;
     
      while (true) {
        int written = local.write(address, buffer, 0, length);
       
        if (written > 0) {
          System.out.println(MessageFormat.format("Trying to send {0} bytes of data to {1}.", written, address));
        } else if (written == 0) {
//          System.out.println(MessageFormat.format("Still trying to send the previously submitted data to {0}, time = {1}.", address, System.currentTimeMillis()));
View Full Code Here

TOP

Related Classes of com.peterhi.net.Local

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.