简体   繁体   English

Java-如何基于时间对tcp服务器中接收到的数据进行排序?

[英]Java- how to sort received data in tcp server based on time?

I want to write a simple java client-server program which has 1 server on a specified PC and 4 clients, each of them on a dedicated PC(each for a user). 我想编写一个简单的Java客户端-服务器程序,该程序在指定的PC上有1个服务器,在4个客户端上分别有一个客户端(每个用户专用)。 the users are going to press the enter button and the server must sort the users based on who entered his button sooner. 用户将要按下回车按钮,服务器必须根据谁较早输入了按钮来对用户进行排序。

the challenge is that in tcp networking, there is no guarantee that the first sent message deliverers sooner than the others. 挑战在于,在tcp网络中,无法保证第一个发送消息的传递者比其他人早。 any idea how to overcome this problem? 任何想法如何克服这个问题?

i think that one way could be sending a timestamp when a client connects to the server. 我认为一种方法可能是客户端连接到服务器时发送时间戳。 With this action you have synchronized all the clients. 通过此操作,您已经同步了所有客户端。 The clients have to count the time passed relative to the server timestamp. 客户端必须计算相对于服务器时间戳经过的时间。 When you have all the traces, despite of the order of traces, you could be able to order by first button clicked. 当您拥有所有迹线时,不管迹线的顺序如何,您都可以通过单击第一个按钮来进行排序。

The problem here is, that latency can not be guaranteed to be same on every network connection. 这里的问题是,不能保证每个网络连接上的延迟都相同。 Therefore, the packet first sent is not guaranteed to arrive first. 因此,不能保证先发送的数据包先到达。

To reduce this problem, I would recommend to do the following: 为了减少此问题,我建议您执行以下操作:

1) You have to synchronize the time on every client with the server (network). 1)您必须将每个客户端上的时间与服务器(网络)同步。 This is a known problem in distributed systems / computing and there are several strategies to do that: http://en.wikipedia.org/wiki/Clock_synchronization 这是分布式系统/计算中的一个已知问题,有几种策略可以做到这一点: http : //en.wikipedia.org/wiki/Clock_synchronization

2) Make sure, your clients have almost the same computing latency (eg processing power) or make sure, that the event on average will be executed in the same speed (button pushed). 2)确保您的客户端具有几乎相同的计算延迟(例如,处理能力),或确保事件平均以相同的速度执行(按下按钮)。

3) Further, you have to monitor the average latency in your network all the time (connection latency clients to server. Button-pushed until queued in the server). 3)此外,您还必须一直监控网络中的平均延迟(客户端到服务器的连接延迟。按下按钮直到在服务器中排队)。

Imagine the following scenario: 想象以下情况:

  • Clocks are synchronised in the network as best as possible. 时钟尽可能在网络中同步。
  • The server knows the average latency from the moment the button was pushed on the client-side until the packet/signal arrives at the server's queue. 从知道从客户端按下按钮到数据包/信号到达服务器队列的平均等待时间,服务器都会知道。

1) Client 1 presses button. 1)客户1按下按钮。 This event get's flagged with a timestamp from the synchronized time/clock. 从同步时间/时钟开始,使用时间戳记标记该事件。 Same with Client 2, but a moment later. 与客户端2相同,但稍后。

2) Client 1 and 2 store the event temporarily and also send the event to the server. 2)客户端1和2临时存储事件,并将事件发送到服务器。

3) Client 2's event arrives earlier at the server. 3)客户端2的事件较早到达服务器。 But the server compares the timestamps that were set when the event button-pushed happened on the client-side instead of taking the queuing order into account. 但是服务器会比较事件按钮在客户端发生时设置的时间戳,而不考虑排队顺序。

3) Server sends acknowledgement to all the clients, telling the winner of the event (button pushed). 3)服务器向所有客户端发送确认,告知事件的获胜者(按下按钮)。

4) If the clients don't receive an acknowledgement after a given period, they resend their event. 4)如果客户在给定时间段内未收到确认,则他们将重新发送事件。

5) If all clients answer the acknowledgement positively, the server can announce the winner. 5)如果所有客户都积极回答了确认,则服务器可以宣布获胜者。 Otherwise, it will be recalculated. 否则,它将被重新计算。

As you see, the entire problem lays at synchronising the clocks. 如您所见,整个问题在于时钟同步。

Hope I could help you, let me know! 希望我能帮助您,让我知道!

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

相关问题 通过 TCP 套接字服务器解析 java 中的二进制数据 - Parsing binary data in java received over TCP socket server java ssl tcp connection ok但收到服务器的数据不正确 - java ssl tcp connection ok but data received to server is incorrect java-根据分配的整数值对字符串的链表进行排序 - java- sort linked list of strings based on their assigned integer values java-如何正确添加时间? - java- how to add time properly? -Java- TCP中的数据“丢失”(奇怪,因为TCP不允许数据泄露) - -Java- Data 'loss' in TCP (strange, since TCP doesn't allow the data to escape) 如何在 HTTP/1.1 中接收分块响应,同时在 Java/Android 中向服务器发送数据 - How to received chunked responses in HTTP/1.1 while sending data at the same time to server in Java/Android Java-根据用户输入从类更新返回数据 - Java- Updating Return Data from a Class based on User Input Java-如何将解析嵌套的json数据存储到Java列表中 - Java- How to store Parsing nested json data into Java list 如何基于从服务器接收的数据在android中渲染视图? - how to render view in android based on the data received from server? Java TCP。 如何确认收到的数据包是一个完整的数据结构? - Java TCP. How to affirm your received packet is a complete data structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM