socket.read() does not work
cool2man opened this issue · comments
My code looks like:
socket = new Socket(host, port);
streamIn = new BufferedInputStream(socket.getInputStream());
c = streamIn.read();
This results always in c = -1 (EOF). On the server side the connection stays on, NO data is sent to the client.
No more infos for the moment, I will continue to check the situation tomorrow. I opened this issue early to give a chance, to look at this topic in combination with the socket.write() problem that I had submitted before.
I checked the situation (using a month older version, but I see no essential changes inside java_net.ts, so I expect the same for the current version):
- standard situation as in my last comment -> read will get -1 at once
- standard situation as in my last comment + in addition I send data from the server to the client -> read will get -1 at once
- standard situation as in my last comment + I set the socket timeout to 30000 -> read will get a -1 after waiting 30 sec.
- standard situation as in my last comment + I set the socket timeout to 30000 + I send data from the server -> read will get the data at once.
- same as last + addition round trip (client sends new data, server answers with new data) -> read will get data data after waiting 30 sec.
This is my version of socketRead0 which works well for me:
public static 'socketRead0(Ljava/io/FileDescriptor;[BIII)I'(thread: JVMThread, javaThis: JVMTypes.java_net_SocketInputStream, fd: JVMTypes.java_io_FileDescriptor, b: JVMTypes.JVMArray<number>, offset: number, len: number, timeout: number): void {
var impl = <JVMTypes.java_net_PlainSocketImpl> javaThis['java/net/SocketInputStream/impl'];
if (impl.$is_shutdown === true) {
thread.throwNewException('Ljava/io/IOException;', 'Socket is shutdown.');
} else {
thread.setStatus(ThreadStatus.ASYNC_WAITING);
if (impl.$ws.rQlen() > 0) { // check if data already available
socket_read_async(impl, b, offset, len, (arg: number) => {
thread.asyncReturn(arg);
});
} else {
var t = 0;
if (timeout) {
t = <number><any> setTimeout(() => { // timeout for data polling
thread.throwNewException('Ljava/net/SocketTimeoutException;', 'Socket timeout.');
}, timeout);
}
impl.$ws.on('message', () => { // fired when data available
if (t) {
clearTimeout(t);
}
impl.$ws.on('message', () => {});
socket_read_async(impl, b, offset, len, (arg: number) => {
thread.asyncReturn(arg);
});
});
}
}
}
A lot of changes were done. This solution is able to run my (home made) web / app server using reverse HTTP connections. One browser session opens up to 5 connections in parallel to the web server which are all running well. For testing purposes I added the use of socket timeout to my implementation, which works also as expected.
I'm happy that you found a solution that works for you. :)
The sockets_fixed branch works locally on a simple test, but is currently failing on Travis. I'd like to figure out why that is before I enhance it further. If you feel like testing it out, let me know if it works for you!
I would check the socket_fixed branch, but "npm install" results into error(s). How is this branch installed and build?
Is it the case that creating a socket is only supported in the browser (outside of that sockets_fixed
branch)? Looking at this conditional.
No. This is the case that the socket implementation in general is currently buggy.