简体   繁体   中英

J2ME app closes socket: Server doesn't get all data

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!

这看起来像是设备中的错误,您可以从J2SE端发送一条消息以确认接收,并在J2ME端等待收到此消息以关闭套接字。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM