简体   繁体   English

如何快速知道服务器是否在线?

[英]How could I quickly know if a server is online?

I'm developing a Java client/server application in which there will be a great number of servers with which the clients are going to have to connect.我正在开发一个 Java 客户端/服务器应用程序,其中将有大量的服务器供客户端连接。 The problem is that probably the vast majority of them will not be serving at the same time.问题是他们中的绝大多数人可能不会同时服务。 The client needs to find at least one available in the list, so it will iterate it, looking for an available server (when it finds the first it stops, one is enough).客户端需要在列表中至少找到一个可用的,所以它会迭代它,寻找一个可用的服务器(当它找到第一个它停止时,一个就足够了)。

The problem is that the list will probably be long, tens of zousands, they could be even hundreds... and it may happen that only 1% of them are connected (ie executing the server).问题是这个列表可能很长,几十个zousands,甚至可能有几百个……而且可能只有1% 的人被连接(即执行服务器)。 That's why I need a clever and a fast way to know if a server is connected, without waiting for time-outs or so.这就是为什么我需要一种聪明且快速的方法来知道服务器是否已连接,而无需等待超时左右。 I accept all kinds of suggestions.我接受各种建议。

I have thought about ordering the server list statistically, so that the servers that are available more often are the first hosts attempted.我曾考虑对服务器列表进行统计排序,以便更频繁地可用的服务器是第一个尝试的主机。 But this is not enough.但这还不够。

Perhaps multicasting UDP datagrams?也许多播UDP数据报? The connections between clients/servers are TCP, but perhaps to find a server it's better to do an UDP multicast first and wait for the answer, for example... what do you think?客户端/服务器之间的连接是 TCP,但也许要找到一个服务器,最好先进行 UDP 多播并等待答案,例如......你怎么看?

:) :)

EDIT:编辑:

Both the server and client use thread pools.服务器和客户端都使用线程池。

The server pool handles 200 threads concurrently, and when the pool is full, queues the rest until the queue is 200 runnables long.服务器池同时处理 200 个线程,当池已满时,将其余线程排队,直到队列长度为 200 个可运行线程。 Then it blocks, and stop accepting connections until there is free room in the queue again.然后它阻塞,并停止接受连接,直到队列中再次有空闲空间。

The client has a cached thread pool, it can make all the request to the server you want concurrently (with common sense, obviously...).客户端有一个缓存的线程池,它可以并发地向你想要的服务器发出所有请求(常识,显然......)。

This is just an initial thought and would add some over head, but you could have the servers periodically ping some centralized server which the clients would connect through.这只是一个初步的想法,会增加一些开销,但是您可以让服务器定期 ping 一些客户端将通过的集中式服务器。 Then if the server doesn't ping for some set time it gets removed.然后,如果服务器在一段时间内没有 ping 通,它就会被删除。

You might want to use a peer-to-peer network.您可能希望使用对等网络。

Have a look at JXTA/JXSE: http://jxse.kenai.com/index.html看看 JXTA/JXSE: http ://jxse.kenai.com/index.html

If it is your own code which is running on each of these servers, could you send an alive to a central server (which is controlled by you and is guaranteed to be up at all times)?如果是您自己的代码在这些服务器中的每一个上运行,您能否将活动发送到中央服务器(由您控制​​并保证始终处于运行状态)? The central server can then maintain an updated list of all servers which are active.中央服务器然后可以维护所有活动服务器的更新列表。 The client just needs a copy of this list from the central server and then start whatever communication it needs.客户端只需要来自中央服务器的这个列表的副本,然后开始它需要的任何通信。

Sounds like a job for Threads.听起来像是 Threads 的工作。 You cannot speed up the connection, it takes time to contact the server.您无法加快连接速度,需要时间来联系服务器。

IMHO, the best way is to get few hundred Threads to march through the list of servers.恕我直言,最好的方法是让几百个线程在服务器列表中前进。 The first one to find one server alive wins.第一个找到一台服务器的人获胜。 Then signal other threads to die out.然后向其他线程发出信号以使其消失。

Btw, did you really mean to order the server list "sadistically"?顺便说一句,你真的想“疯狂地”订购服务器列表吗? :) :)

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

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