views:

24

answers:

1

Hi Folks!

I have a problem with an J2ME client app, that sends data to an J2SE server, and immediately closes the sending socket. On the J2ME side, i use a ordinary OutputStream on a SocketConnection, and repeatedly call write with small packets of data (~30 bytes). Afterwards, I flush and finally close the stream and the connection.

When running the client in the emulator, everything works fine. But with the real device I get some problems...

What I noticed is that the connection is not correctly closed, no matter what I do on the client. I always get an Connection reset exception on the server, which according to TCP indicates an error in the connection or sender, meaning that all subsequent data is to be abandoned and the connection no longer to be used. (With the emulator, the read on the server eventually returns -1, indicating that the connection was correctly closed, no exception at all...)

I tried to play with the total packet size (1024, 2048, ...) and with the client side socket options (Delay, Linger, Keep alive). I also tried a Thread.sleep between flush and close... On the server side, different things happen:

  • Only the first packet of around 30 byte is received, then exception (w/o delay)
  • Part of the data is received (~1500 bytes), then no more data is read, and no exception thrown, blocking in the read method forever (with delay and total size around 2048)
  • All data is correctly received, then exception (with delay and total size around 1024)

In all cases, the client has successfully sent the whole data.

What is the best way to ensure that all data will be received by the other side? As I said, the J2ME client states that all data was successfully written! (The total size can't be fixed to a specific value)

Thanks in advance!

A: 

That looks like a bug in your device you can send a message from the J2SE side to acknowledge the reception and wait on the J2ME side that you receive this message to close the socket.

Xavier Combelle
I was hoping there was a possibility without having the mobile app to react... well I added a special stop-byte and it's working now. I even don't get the exception anymore, as the connection is correctly closed! However, I thought the Linger option was waiting in the background after closing a connection until all data is ACKed...
king_nak