Am Dienstag, 31. Juli 2012 18:30:22 UTC+2 schrieb Joel Dice:
On Tue, 31 Jul 2012, Remi wrote:
> InputStream created from a ServerSocketChannel.accept() always returns
> available() = 0, but read can be performed and bytes will be returned
Can you be more specific? ServerSocketChannel.accept returns a
SocketChannel, not an InputStream, and the only way to convert that to an
InputStream is using Channels.newInputStream (...) Is that what you're doing?
Yes, this is what I'm doing. I thought I was specific enough, because there is no another way to do it, isn't it? :)
// create the server socket
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress("10.0.2.3", 80)); // On sun-vm I can specify new InetSocketAddress((InetAddress)null, 80) to bind to all interfaces, but here in Avian I need to pass an IP-Address.... :(
// listen for a connection
SocketChannel socketchannel = serverSocketChannel.accept();
// Now I'm connecting with Chrome to that socket. 10.0.2.3 is my own IP
// After the connection established, I receive a SocketChannel object. Then I do..
InputStream inputStream = Channels.newInputStream(socketChannel);
// then in a loop I'm reading the incoming HTTP-Header
for(;;) {
int n = inputStream.available();
// n = 1024;
if (n > 0) {
// read it here...
byte[] data = new byte[n];
int readBytes = inputStream.read(data)
(...)
} else {
// relax
Thread.sleep(1);
}
}
==> n is always = 0
==> but if I set n to 1024 to enter the if (n > 0), then read(data) reads the incoming bytes and returns the correct read number of bytes (in my case there are 417 bytes)
in sun-vm available behaves correctly and returns the correct number of available bytes