使用 MDS 与 Blackberry 一起使用 SocketConnection 时出现问题

发布于 2024-08-02 08:37:51 字数 916 浏览 6 评论 0原文

我目前正在 Blackberry 上编写一个应用程序,用于将一些原始数据简单地发送和接收到网络上另一个基于 TCP 的设备。我在运行 MDS 模拟器并使用物理电话与我公司的 MDS 服务器通信的黑莓模拟器中遇到了同样的问题。请注意,直接使用 wifi 而不是通过 MDS 时不会出现此问题。

问题是,除非我先调用 read(),否则 InputStream 上的 available() 函数将返回零。如果我先调用 read (知道有一些可用数据..谢谢wireshark),数据就会返回,随后对 available() 的调用会指示还剩下哪些我没有读取的数据。问题是我并不总是能保证数据会在那里,所以我可以阻止。有人意识到这一点吗?这是一个问题还是设计使然?

有谁知道一种方法来测试 read() 方法是否会在调用可用之外的方法之前阻塞?

这基本上就是我正在做的事情:

SocketConnection s = (SocketConnection)Connector.open("socket://1.2.3.4:port;deviceside=false", Connector.READ_WRITE);

OutputStream o = ((StreamConnection)s).openOutputStream();
InputStream i = ((StreamConnection)s).openInputStream();

o.write("hello");
Thread.sleep(sometime);
if (i.available() > 0) {
   byte[] data = new data[10];
   int bytesRead = i.read(data);
   System.out.println("Read [" + new String(data) + "] (bytes = " + bytesRead + ")");
}

我必须注释掉 if 条件才能使其工作。

I am currently writing an app on the Blackberry to do a simple send and receive of some raw data to another TCP based device on my network. I am having the same problem in the Blackberry simulator w/ an MDS simulator running and using a physical phone talking to my company's MDS server. Note this problem does not happen when using wifi directly and not via MDS.

The problem is that the available() function on the InputStream returns zero unless I call read() first. If I call read first (knowing there is some data available .. thank you wireshark) the data comes back, and the subsequent call to available() indicates what data is left that I did not read. The problem is that I am not always going to be guaranteed that data will be there and so I could block. Is anyone aware of this, and is this a problem or something that is by design?

Is anyone aware of a way to test if the read() method(s) will block before calling them aside from available?

Here is basically what I am doing:

SocketConnection s = (SocketConnection)Connector.open("socket://1.2.3.4:port;deviceside=false", Connector.READ_WRITE);

OutputStream o = ((StreamConnection)s).openOutputStream();
InputStream i = ((StreamConnection)s).openInputStream();

o.write("hello");
Thread.sleep(sometime);
if (i.available() > 0) {
   byte[] data = new data[10];
   int bytesRead = i.read(data);
   System.out.println("Read [" + new String(data) + "] (bytes = " + bytesRead + ")");
}

I have to comment out the if conditional for this to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住我的手 2024-08-09 08:37:51

InputStream.available() 方法的一般约定是,它“返回可以从此输入流读取(或跳过)的字节数,而不会被该输入流的方法的下一个调用者阻塞”。因此,在大多数实现中,不能保证它将返回正在读取的流的内容长度。因此,最好按以下方式读取它:

byte[] readFromStream(InputStream is) throws IOException
{
    byte[] data = new byte[4096];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    int count = is.read(data, 0, data.length);
    while (count != -1)
    {
        dos.write(data, 0, count);
        count = is.read(data, 0, data.length);
    }

    data = baos.toByteArray();

    return data;
}

调用 readFromStream() 方法并获取返回的 byte[]。

The general contract of the InputStream.available() method is that it "Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream." Hence in most implementations, it is no guarantee that it will return the Content Length of the stream that is being read. Hence it is better to read it in the following way

byte[] readFromStream(InputStream is) throws IOException
{
    byte[] data = new byte[4096];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    int count = is.read(data, 0, data.length);
    while (count != -1)
    {
        dos.write(data, 0, count);
        count = is.read(data, 0, data.length);
    }

    data = baos.toByteArray();

    return data;
}

You call the readFromStream() method and get the byte[] returned.

ˇ宁静的妩媚 2024-08-09 08:37:51

正如我在上面的评论中指出的,我需要一种方法来确定我正在连接的设备是否不存在,我通过查看“ping”是否返回任何数据来做到这一点。如果设备不存在,它将阻塞。我不能依赖这种行为。解决此问题时出现的另一个问题是,如果您提供的缓冲区大于您想要返回的数据,则 RIM InputStream 类的 read(...) 方法会阻塞。但是如果 available() 返回 0,我该如何知道有多少数据呢?逐字节读取是执行此操作的唯一方法,但如果没有数据,它仍然会阻塞。

为了解决这个问题,我遵循了第一个答案的主题,但我将此方法放在自己的线程上,并将其写入单独的字节缓冲区。我创建了一个扩展 InputStream 并实现 available() 和 read(...) 的类。可用返回字节缓冲区中有多少字节,只读返回缓冲区中有多少字节或调用者请求多少字节,以较小者为准。

此设置允许我使用 InputStream 接口,但在幕后它只是一个持续运行的读取器线程,在连接断开之前一直处于活动状态。此时读取如果被阻塞,将抛出异常以指示连接已关闭。这种行为很好,因为它很容易处理。

感谢以上所有帮助解决此问题的人。您的想法有助于找到解决方案。

As I indicated in a comment above, I needed a way to determine if a device I am connecting to is not there, and I do that by seeing if our 'ping' returns any data. If the device is not there it will block. I cannot rely on that behavior. Another issue that crept up while solving this is that the read(...) methods of the RIM InputStream class block if you provide a buffer bigger than the data you want back. But how am I supposed to know how much data is there if available() returns 0? Reading byte-by-byte is about the only way to do this, but it still blocks if there is no data.

To address this I followed the theme of the 1st answer, but I put this method on its own thread and had it write to a separate byte buffer. I created a class that extended InputStream and implemented available() and read(...). Available returns how many bytes are in the byte buffer, and read only gives back however much is in the buffer or however much the caller requests, whichever is less.

This setup lets me use an InputStream interface, but behind the scenes it is just a continuously running reader thread that is alive until the connection is dropped. At that time the read, if blocked, will throw an exception to indicate the connection closed. This behavior is fine as it can be easily handled.

Thanks to all above who helped with this issue. Your thoughts help move towards the solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文