简体   繁体   中英

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). 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. 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). This is a known problem in distributed systems / computing and there are several strategies to do that: 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).

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).

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. This event get's flagged with a timestamp from the synchronized time/clock. Same with Client 2, but a moment later.

2) Client 1 and 2 store the event temporarily and also send the event to the server.

3) Client 2's event arrives earlier at the server. 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).

4) If the clients don't receive an acknowledgement after a given period, they resend their event.

5) If all clients answer the acknowledgement positively, the server can announce the winner. Otherwise, it will be recalculated.

As you see, the entire problem lays at synchronising the clocks.

Hope I could help you, let me know!

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