Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.read() does not work #485

Open
cool2man opened this issue Oct 26, 2016 · 6 comments
Open

socket.read() does not work #485

cool2man opened this issue Oct 26, 2016 · 6 comments
Labels

Comments

@cool2man
Copy link

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.

@jvilk jvilk added the bug label Oct 26, 2016
@cool2man
Copy link
Author

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.

@cool2man
Copy link
Author

cool2man commented Nov 2, 2016

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.

@jvilk
Copy link
Member

jvilk commented Nov 10, 2016

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!

@cool2man
Copy link
Author

I would check the socket_fixed branch, but "npm install" results into error(s). How is this branch installed and build?

@semperos
Copy link

Is it the case that creating a socket is only supported in the browser (outside of that sockets_fixed branch)? Looking at this conditional.

@cool2man
Copy link
Author

No. This is the case that the socket implementation in general is currently buggy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants