简体   繁体   English

J2ME应用程序关闭套接字:服务器未获取所有数据

[英]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. 我的J2ME客户端应用程序有问题,该应用程序将数据发送到J2SE服务器,并立即关闭发送套接字。 On the J2ME side, i use a ordinary OutputStream on a SocketConnection , and repeatedly call write with small packets of data (~30 bytes). 在J2ME端,我在SocketConnection上使用普通的OutputStream ,并重复调用带有少量数据包(约30个字节)的write Afterwards, I flush and finally close the stream and the connection. 之后,我flush并最终close流和连接。

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. 我总是在服务器上收到一个“ Connection reset异常,根据TCP的指示,该异常表示连接或发送方中存在错误,这意味着所有后续数据都将被放弃,并且连接将不再使用。 (With the emulator, the read on the server eventually returns -1 , indicating that the connection was correctly closed, no exception at all...) (使用仿真器,服务器上的read最终返回-1 ,表明连接已正确关闭,一点也不例外...)

I tried to play with the total packet size (1024, 2048, ...) and with the client side socket options (Delay, Linger, Keep alive). 我尝试使用总数据包大小(1024、2048等)和客户端套接字选项(延迟,延迟,保持活动)。 I also tried a Thread.sleep between flush and close ... On the server side, different things happen: 我还尝试在flushclose之间执行Thread.sleep 。在服务器端,发生了不同的事情:

  • Only the first packet of around 30 byte is received, then exception (w/o delay) 仅接收到大约30个字节的第一个数据包,然后接收到异常(无延迟)
  • 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) 接收到部分数据(约1500字节),然后不再读取任何数据,也没有引发异常,从而永久阻塞了read方法(延迟和总大小约为2048)
  • All data is correctly received, then exception (with delay and total size around 1024) 正确接收所有数据,然后接收异常(延迟和总大小约为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! 就像我说的那样,J2ME客户端声明所有数据都已成功写入! (The total size can't be fixed to a specific value) (总大小无法固定为特定值)

Thanks in advance! 提前致谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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