Java ServerSocket 和使用 ObjectIOStreams 的套接字会丢失数据包吗?

发布于 2024-08-31 02:53:06 字数 472 浏览 2 评论 0原文

我在服务器上使用 ServerSocket,并使用 ObjectIOStreams 通过网络连接发送可序列化对象的套接字。我正在开发一种本质上更加金融化的垄断版本,因此需要发送数据包并确认发送/接收。我是否需要实现自己的数据包丢失观察程序,或者是否已经通过(服务器)Sockets 解决了这个问题?

我主要是问在网络故障或其他什么情况下丢失数据包,而不是完全连接错误。例如,兄弟姐妹在我的路由器和计算机的 Wi-Fi 适配器之间移动铅板。

http://code.google.com/p /inequity/source/browse/#svn/trunk/src/network 代码可以在网络->ClientController和网络->Server下找到

I'm using a ServerSocket on my server and Sockets that use ObjectIOStreams to send serializable objects over the network connection. I'm developing an essentially more financial version of monopoly and thus packets being sent and confirmed as sent/received is required. Do I need to implement my own packet loss watcher or is that already taken care of with (Server)Sockets?

I'm primarily asking about losing packets during network blips or whatnot, not full connection error. E.g. siblings move a lead plate between my router and computer's wi-fi adapter.

http://code.google.com/p/inequity/source/browse/#svn/trunk/src/network
Code can be found under network->ClientController and network->Server

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-09-07 02:53:06

理论上;是的。理论上无法 100% 保证在硬件层发送的内容在接收端以相同的方式接收。

但实际上,如果您使用 TCP(传输控制协议),这些问题已经得到处理;你不会丢失任何数据包。 (如果您使用的是 UDP(用户数据报协议),那就另当别论了,很可能您会丢失数据包,或者乱序接收数据包)。

只需简单地查看一下您的代码,您似乎正在使用多个线程。如果是这样,您必须非常小心同步。很可能的情况是,看起来数据包已被丢弃,但由于程序中的竞争条件,它根本没有得到处理。 (请记住,例如 gui,在其自己的线程中运行。)

我认为,解决同步问题的最佳方法是将网络循环放入一个非常小的读取/放入同步队列循环中,并且每当您确定没有其他线程介入时,就从队列中拾取收到的数据包。

Theoretically; yes. There is no way of giving 100 % theoretical guarantee that what is sent on the hardware layer, is received the same way on the receiving end.

Practically however, if you use TCP (Transmission Control Protocol) this stuff has already been taken care of; you won't loose any packets. (If you're using UDP on the other hand (User Datagram Protocol) it's another story, and it may very well be the case that you're loosing packets, or receiving them out of order).

Just looked briefly at your code, and it seems you're using multiple threads. If so you must be utterly careful with synchronization. It could very well be the case that it looks like a packet has been dropped, although, it is simply not handled due to a race condition in the program. (Keep in mind that the gui for instance, runs in its own thread.)

The best way to solve the synchronization, I think, is to put the network loop in a very small read/put-on-synchronized-queue loop, and pick up the received packets from the queue whenever you're sure no other thread will intervene.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文