简体   繁体   English

运行服务器的Java Lan游戏列表IP地址

[英]Java Lan Game list Ip addresses of Running Server

We are currently working on a client-server game in java. 我们目前正在使用Java开发客户端/服务器游戏。 We used DatagramSocket to perform communications between client and server. 我们使用DatagramSocket在客户端和服务器之间执行通信。 Inorder for the client to communicate to server, he must know the LAN ip address of the machine where the server is running. 为了使客户端与服务器通信,他必须知道运行服务器的计算机的LAN ip地址。 Multiple clients can connect to server, we used a thread for each client. 多个客户端可以连接到服务器,我们为每个客户端使用了一个线程。

Our problem is we wanted to show a list of LAN ip addresses of running servers to the clients so that it is easier for them to find servers like slist command in Counter Strike. 我们的问题是我们想向客户端显示正在运行的服务器的LAN ip地址列表,以便客户端可以更轻松地在Counter Strike中找到服务器,例如slist命令。 Our solution is the client sends a message to all computers connected to the network and if a computer with a running server receives it, it will reply a message back to the client and then we print it's ip address in the client. 我们的解决方案是客户端向连接到网络的所有计算机发送消息,如果一台运行服务器的计算机收到该消息,它将回复消息给客户端,然后我们在客户端中打印其IP地址。

InetAddress localHost = Inet4Address.getLocalHost();
String myIP=localHost.toString().substring(localHost.toString().lastIndexOf("/")+1);

//split myIP into 4 parts (part1.part2.part3.part4)
//use the first part to check the class of the network (A, B, or C)

//I'll skip to C
if(part1>=1 && part1<=126) //A
else if(part1>=128 && part1<=191) //B
else if(part1>=192 && part1<=223){
    String network= part1 + "." + part2 + "." part3;
    String guess;
    for(i=0;i<255;i++){
         guess = network+"."+i;
         serverSend(guess); //send the message to an ip address in the network
    }
}

It works fine for network in class C but for B and A, it is too slow. 对于C类网络,它工作正常,但对于B和A,速度太慢。 We assume the worst-case subnet mask for B and A which is 255.255.0.0 (65,025 iterations) and 255.0.0.0 (16,581,375). 我们假设B和A的最坏情况的子网掩码为255.255.0.0(65,025次迭代)和255.0.0.0(16,581,375)。

Does anyone got a better solution to this? 有人对此有更好的解决方案吗? thank you in advance. 先感谢您。

what you are looking for is broadcast. 您正在寻找的是广播。

I have gathered a tutorial for you: http://download.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html 我已经为您收集了一个教程: http : //download.oracle.com/javase/tutorial/networking/datagrams/broadcasting.html

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

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