Package com.peterhi.net2

Source Code of com.peterhi.net2.ClientApp

package com.peterhi.net2;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;

import com.peterhi.net2.Endpoint.EndpointEvent;
import com.peterhi.net2.Endpoint.EndpointStateListener;

public class ClientApp {

  public static void main(String[] args) throws Exception {
    Endpoint endpoint = new Endpoint(0);
    endpoint.addEndpointStateListener(new EndpointStateListener() {
      @Override
      public void started(EndpointEvent event) {
        Endpoint endpoint = (Endpoint )event.getSource();
        InetAddress address;
       
        try {
          address = InetAddress.getLocalHost();
        } catch (UnknownHostException ex) {
          ex.printStackTrace();
          return;
        }
       
        SocketAddress socketAddress = new InetSocketAddress(address, 22222);
        String string = "Hello World!";
        byte[] data = string.getBytes();
       
        try {
          endpoint.send(socketAddress, data, 0, data.length);
        } catch (IOException ex) {
          ex.printStackTrace();
        }
      }

      @Override
      public void stopped(EndpointEvent event) {
      }
    });
    Thread thread = new Thread(endpoint);
    thread.start();
   
    System.out.print("Press any key to continue.");
    System.in.read();
    System.exit(0);
  }
 
}
TOP

Related Classes of com.peterhi.net2.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.