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!