Hi,
I've got this kind of code to port to Java:
public class MyClass
{
Stream _stream;
AsyncCallback callback;
IAsyncResult readOperation;
public MyClass(string host, string port)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(host, port);
_stream = tcpClient.GetStream();
callback = new AsyncCallback(Read);
readOp = _stream.BeginRead(headerBuffer, 0, 1, callback, null);
}
private void listen(IAsyncResult asyncResult)
{
_stream.EndRead(asyncResult);
//Do some work
readOperation = _stream.BeginRead(headerBuffer, 0, 1, callback, null);
}
}
I've tried this with no real success
es = Executors.newFixedThreadPool(2);
es.execute(new ListenTask());
and
public class ListenTask implements Runnable {
public void run() {
//Stream is a SocketChannel
_stream.socket().getInputStream().read(headerBuffer, 0, 1);
}
}
Should I use SocketChannel.read() ?
Bonus question : What would be the java equivalent of readOperation.AsyncWaitHandle.WaitOne();
Thank you very much