How to read and write network sockets in Java ME on a device with simplex connectivity?

I am working on a mobile communicator and after establishing connection to the server using following method (to illustrate that I am using **StreamConnection**, **InputStream** and **OutputStream**) I distribute **inputStream** and **outputStream** between two separate threads, lets call them **Sender** and **Receiver**. Connection method: private InputStream inputStream; private OutputStream outputStream; private StreamConnection connection; public void connect(String host, String port) throws IOException { String connectionString = "socket://" + host + ":" + port; connection = (StreamConnection) Connector.open(connectionString); inputStream = connection.openDataInputStream(); outputStream = connection.openOutputStream(); } **Sender** thread is waiting for anything to appear in the out buffer. When output buffer is empty, **Sender** waits (by calling `wait()` method on sender thread). Any input into the output buffer calls `notify()` method on the sending thread. **Receiver** thread polls the **InputStream** using `int available()` method and when there is something to receive, it calls blocking `int read()` method. Everything works like a charm in various emulators and few devicdes I have handy around. However there is one phone that seems to missbehave. Whenever one thread calls `available()` or `read()` on **InputThread** object, while the other thread calls `write()` on the **OutputStream** object, the Input stream finishes. All subsequent reads will return value **-1**, which means that the InputStream got closed. After massive google-fu, I came accross **[this post on nokia forums][1]**, where simplex/duplex properties of a device are being discussed and that seems to be the case with the device I have troubles with. Normally (99% of the time) calls to read() and write() can be simultaneous without any problems. My question is then: Did anybody came accross similar problems? How did/would you sort out the issue of one thread independently reading, while another is independently writing into the established connection, so that they do not call read() or available() while calling write()? Any pointers in any directions greatly appreciated! [1]: http://discussion.forum.nokia.com/forum/archive/index.php/t-37472.html
you might want to add j2me and javame tags to your question

以上就是How to read and write network sockets in Java ME on a device with simplex connectivity?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏