J2ME - cannot disconnect client-side output, server freezes while waiting for data [nokia J2ME implementation]

My question is: is there a way to turn off the OutputStream socket, or is it incorrectly / fully implemented, as Nokia should have? (J2ME implementation of nokia, tested on nokia c6-00 and not closing the stream, tested on emulator and working fine)

The main problem is that the J2SE server application does not receive information about the stream, the read condition (buffer) == -1 is never true, it tries to read from an empty stream and freezes until the client is killed by force. This works with a very, very, very ugly server-side workaround.

        Thread.sleep(10);//wait some time for data else you would get stuck........
        while ((count = dataInputStream.read(buffer)) != -1) {
            byteArrayOutputStream.write(buffer, 0, count);
            if (count != BUFFER_SIZE_1024 || dataInputStream.available() == 0) { //the worlds worst condition ever written... but works
                break;
            }
            Thread.sleep(10);//wait for data input to get some data for dataInputStream.available() to return != 0 if client still sends data else you would not read all data......
        }

but this solution is absolutely unacceptable (I don’t know something about nokia java encoding, I missed something, or it looks like some kind of nokia-J2ME encoding standard, and I have to get used to it or change the platform)

I cannot close the client socket after sending the data, because the server sends a response to the client after receiving and processing the data.

It looks like this: J2ME client -> J2SE server (freezes when reading because the client does not shutdown the output stream) -> J2ME

I tried: close dataOutputStream on the J2ME client - no setSocketOptions effect (KEEPALIVE, SNDBUF and others) - no effect or errors

nothing works on the target device

Sorry, but now I'm a little furious after this pointless fight with little java.

I was looking for a solution but not working

Client Code:

        SocketConnection socketConnection = (SocketConnection) Connector.open("socket://" + ip + ":" + port);
        int count;
        byte[] buffer = new byte[BUFFER_SIZE_1024];
        // client -> server
        DataOutputStream dataOutputStream = new DataOutputStream(socketConnection.openDataOutputStream());
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        while ((count = byteArrayInputStream.read(buffer)) != -1) {
            dataOutputStream.write(buffer, 0, count);
            dataOutputStream.flush();
        }
        dataOutputStream.close();
        byteArrayInputStream.close();
+3
2

- , , :

J2ME 1024- (IsNext IsLast) J2SE while(true). readShort, readByte ( , , , , , , , , , , , ).

, [ while (dataInputStream.available() < length) {} - -, . , J2ME dataInputStream.available() 0 (!), J2ME, , for (int i = 0; i < length... loop, ]

while(dataInputStream.available() ... , , , IsLast, while(true). .

, -

+1

J2SE Socket java.nio.channels.SocketChannel .

, , , J2ME - .

, , , . while - , .

:

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

    try
    {
        DataOutputStream dataOutputStream = new DataOutputStream(
                  socketConnection.openDataOutputStream()
                  );
        try
        {
            while ((count = byteArrayInputStream.read(buffer)) != -1) {
                dataOutputStream.write(buffer, 0, count);
                dataOutputStream.flush();
            }
        }
        finally
        {
            dataOutputStream.close();
        }
    }
    finally
    {
        byteArrayInputStream.close();
    }

, ByteArrayInputStream, , - -, .

+2

All Articles