3

我的问题是:有没有办法执行套接字 OutputStream 关​​闭,或者它不正确/完全实现,因为它应该由诺基亚实现?(J2ME nokia 实现,在 nokia c6-00 测试并且没有关闭流,在模拟器上测试并且工作正常)

主要问题是 J2SE 服务器应用程序没有获得流信息的结尾,条件 read(buffer) == -1 永远不会为真,尝试从空流中读取并挂起,直到客户端被强制终止。这适用于服务器端应用程序上非常非常非常丑陋的解决方法

        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......
        }

但是这个解决方案是绝对不能接受的(我对诺基亚 java 编码一无所知,我遗漏了一些东西,或者它可能类似于某种 nokia-J2ME 编码标准,我应该习惯它或改变平台)

发送数据后我无法关闭客户端套接字,因为服务器在接收和处理数据后向客户端发送响应。

它看起来像这样:J2ME 客户端 -> J2SE 服务器(由于客户端不执行输出流关闭而挂起读取) -> J2ME

我尝试过:关闭 J2ME 客户端上的 dataOutputStream - 无效果 setSocketOptions(KEEPALIVE、SNDBUF 等) - 无效果或错误

目标设备上似乎没有任何效果

抱歉,在与小 Java 进行了这场无意义的战斗之后,我现在有点生气。

我已经寻找解决方案,但似乎没有工作

客户端代码:

        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();
4

2 回答 2

2

对于 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 并不是绝对必要的,但代码有变异的习惯,有一天输入流可能会变成需要显式关闭的东西。

于 2012-04-27T18:48:36.913 回答
1

我已经尝试过具有相同效果的代码 - 在模拟器上就像一个魅力,在设备上挂起但我解决了我的问题如下:

On the J2ME client before sending the 1024 byte packet I'm sending its length and its state (IsNext or IsLast) after this on the J2SE server side in a while(true) loop. I'm reading first the length with a readShort, then state with a readByte (I know it's better to combine it on a one short but I didn't knew if it will work and if the effort was worth it and now when it works I'm not touching this, besides it is easy to add a new state if necessarily and it works quite fast).

After this server goes in to a second nested loop [ while (dataInputStream.available() < length) {} - I'll have to put here a timeout but I'll worry about that later. Also note that on J2ME dataInputStream.available() always returns a 0 (!) so in the J2ME client read in this place is a for (int i = 0; i < length... loop reading a single byte]

while(dataInputStream.available() ...循环中断时,我正在读取我有长度的数据块,如果状态是IsLast我打破while(true)循环。工作完美且稳定。

感谢您的建议,希望此信息对某人有所帮助

于 2012-04-29T00:32:12.437 回答