views:

670

answers:

2

Dear all,

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, 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!

A: 

Between that kind of problem and mobile network operators filterning data, most mobile java developers use the "http://" protocol instead of "socket://".

Of course, that means not using duplex connection anymore and making many GET and POST requests instead.

Far from ideal, I know.

QuickRecipesOnSymbianOS
+1  A: 

Normally, very little is guaranteed when we talk about multi-threaded applications. I would recommend to use a single thread in your application and try to manage it using a single worker. Some devices do have behavioral problems and so what we see on one device does not appear to work on some other device.

I have worked a lot with mobile games where a lot of animation has to be rendered without compromising the speed of the game. I have realized that you can do more with a single thread and it makes your application very portable (with almost no changes).

If you are waiting for the threads to complete either READ or WRITE operation, so it looks like you are actually doing this sequentially. So more or less, things would be complicated with more than one thread. Instead, built a wait-notify mechanism, based on some predetermined factor and allow the SINGLE thread to either read or write to the socket stream. Switching between threads is a very costly operation, than this scheme. Hope this answers your question.

Gaurav Saini