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);
}
}