package com.peterhi.net7;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.MessageFormat;
import com.peterhi.net.Event;
import com.peterhi.net.Listener;
import com.peterhi.net.Local;
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()));
} else if (written < 0) {
System.out.println("Finished writing.");
break;
}
Thread.sleep(10);
}
// }
/*String string = "Hello World!";
byte[] bytes = string.getBytes();
local.send(address, bytes, 0, bytes.length);*/
}
}