如何通过Kryonet LAN搜索检查要连接的特定服务器?

时间:2016-04-23 03:16:58

标签: java networking slick2d kryonet

我正在使用Slick2D制作游戏逻辑和Kryo​​net进行网络游戏。游戏可以通过局域网在2人多人游戏中进行,其中一个玩家是主机服务器而另一个玩家是接收有关渲染内容的信息的客户端。

设置主机时,第一个播放器输入服务器的名称,然后第二个播放器(客户端)应在该特定端口上搜索LAN上的服务器。我的问题是我只知道Kryonet的主机发现内置方法

import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class PrintToFile { //open class 

private static Formatter output;

public static void main (String args[]) throws IOException { //open main     
openFile();
addRecords();
closeFile();

BufferedReader printFile = new BufferedReader(new FileReader("YoastReginaITM251Project9.txt"));

for (String line; (line = printFile.readLine()) != null;) { //open for 
  System.out.println("Text: " + line);
  System.out.println("Text in Upper Case: " + line.toUpperCase());
  System.out.println("Text in Lower Case: " + line.toLowerCase());
  System.out.println("Text in Reverse Case: " + line);
} //close for

} //close main 

public static void openFile() { //open openFile

try { //open try 
  output = new Formatter("PrintToFile.txt"); //open file 
} //close try 

 catch (SecurityException securityException) { //open catch 
  System.err.println("Write permission denied. Terminating.");
  System.exit(1);
} //close catch 

catch (FileNotFoundException fileNotFoundException) { //open catch 
  System.err.println("Error opening file. Terminating.");
  System.exit(1);
} //close catch 

} //close openFile

public static void addRecords() { //open addRecords 


  try { //open try 
    output.format("%s", input.nextLine()); 
  } //close try 
  catch (FormatterClosedException formatterClosedException) { //open catch 
    System.err.println("Error writing to file. Terminating.");
  } //close catch 
  catch (NoSuchElementException elementExpcetion) { //open catch 
    System.err.println("Invalid input. Please try again.");
    input.nextLine(); 
  } //close catch 


  } //close AddRecords 

 public static void closeFile() { //open closeFile 

  if (output != null)
  output.close();

  } //close closeFile   

 } //close class  

将在指定端口上找到第一台通过LAN运行的服务器。但是,一旦我检查此服务器并发送确认名称的数据包,如果它不匹配,如何找到另一台服务器而没有找到我刚检查过的同一台服务器?

1 个答案:

答案 0 :(得分:0)

没关系!只是仔细查看Kryonet库,发现discoverHosts()有另一个返回InetAddresses列表的方法。我想我可以通过地址,发送确认名称的数据包,当我点击正确的服务器时,保持连接。