Package com.peterhi

Source Code of com.peterhi.ClientApp

package com.peterhi;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class ClientApp {

  public static void main(String[] args) throws Exception {
    ScheduledExecutorService receiver = Executors.newSingleThreadScheduledExecutor();
    ScheduledExecutorService sender = Executors.newSingleThreadScheduledExecutor();
    ScheduledExecutorService user = Executors.newSingleThreadScheduledExecutor();
    final LocalEndpoint endpoint = new LocalEndpoint();
   
    receiver.submit(new Callable<Object>() {
      @Override
      public Object call() throws Exception {
        while (true) {
          try {
          DatagramPacket datagram = endpoint.doReceive();
         
          if (datagram != null) {
            endpoint.doProcess(datagram);
          }
         
          Thread.sleep(10);
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    });
   
    sender.submit(new Callable<Object>() {
      @Override
      public Object call() throws Exception {
        while (true) {
          try {
          endpoint.doDispatch();
          Thread.sleep(10);
          } catch (Exception ex) {
            ex.printStackTrace();
          }
        }
      }
    });
   
    user.submit(new Callable<Object>() {
      @Override
      public Object call() throws Exception {
        try {
        InetAddress ip = InetAddress.getLocalHost();
        int port = 22222;
        SocketAddress address = new InetSocketAddress(ip, port);
        String hello = "Hello World!";
        byte[] data = hello.getBytes();
        endpoint.send(address, data, 0, data.length);
        StringBuilder sb = new StringBuilder();
       
//        for (int i = 0; i < 2796202; i++) {
        for (int i = 0; i < 12000; i++) {
          sb.append("Hello World!");
        }
       
        byte[] data2 = sb.toString().getBytes();
       
        while (true) {
          int status = endpoint.write(address, data2, 0, data2.length);
         
          if (status == RUDP.STATUS_BLOCKING) {
            Thread.sleep(50);
            continue;
          } else if (status == RUDP.STATUS_ACKNOWLEDGED) {
            break;
          }
        }
        } catch (Exception ex) {
          ex.printStackTrace();
        }
       
        return null;
      }
    });
  }
 
}
TOP

Related Classes of com.peterhi.ClientApp

TOP
Copyright © 2018 www.massapi.com. 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.