我有一个具有不同FPS游戏IP地址的数据库,我需要在5分钟内查询每台服务器5。通过查询消息响应(从服务器接收),我知道服务器是否在线,它有多少玩家以及其他一些数据。
我的问题是并非所有服务器都在响应查询。
// Starting a listen server before sending any data to the servers.
new Thread(new ListenServer(socket)).start();
// Connecting and getting data from data base
MysqlData data = new MysqlData("localhost", "root", "abc123", "gm_database");
LinkedList<Map> query_data = data.mysql_fetch_assoc("SELECT * FROM gm_servers");
// Sending 16 bytes of data (Query message) for each server
for(Map<Integer, String> m : query_data){
// m.get(0) is IP address, and m.get(1) is PORT
send_packet = new DatagramPacket(buff, buff.length, InetAddress.getByName(m.get(0)), Integer.parseInt(m.get(1)));
socket.send(send_packet);
}
System.out.println("Query header sent to " + query_data.size() + " servers. Total: " + query_data.size() * buff.length + " bytes of data.");
输出:
发送到1200台服务器的查询标头。总计:19200字节的数据。
ListenServer类(正在侦听60秒以获取服务器的所有响应):
public class ListenServer implements Runnable{
private DatagramSocket socket;
public ListenServer(DatagramSocket socket){
this.socket = socket;
}
@Override
public void run(){
long startTime = System.currentTimeMillis();
int iServerResponse = 0;
// While is running for 60 seconds
while(System.currentTimeMillis < startTime + 60000){
byte[] buffer = new byte[256];
DatagramPacket receive_packet = new DatagramPacket(buffer, buffer.length);
socket.receive(receive_packet);
iServerResponse++;
// Reading bytes and other things...
}
System.out.println("Received response from " + iServerResponse + "servers.");
}
}
输出:
收到651台服务器的回复。
如您所见,并非所有服务器都对查询做出响应。如果我进行手动检查,我会收到回复。
我做错了什么?我需要为每个服务器(1200+ Threads + ExecutorServer等)启动X线程吗?我认为它是for-each循环,我为太多地址发送了太多数据。或者它可能是监听服务器类。