[英]Java networking - java.net.BindException
I'm creating a LWJGL Strategy Game, and I'm implementing multiplayer into it. 我正在创建一个LWJGL策略游戏,并且正在其中实现多人游戏。
Right now the game is just generating a world with some different tile types. 现在,该游戏正在生成一个具有一些不同图块类型的世界。
I thought I should start implementing networking now, to make the server generate the world,and all clients joining download that world and load it (even though the game is barely a playable yet) to make it easier to implement more advanced stuff later on. 我想我应该立即开始实施网络,以使服务器产生世界,并且所有加入该市场的客户都下载并加载该世界(即使游戏还不能玩),以便以后更轻松地实现更高级的东西。 Now to the problem!
现在问题了!
I'm watching these tutorials on networking implementation, made by DesignsbyZephyr , but I'm getting this error: 我正在观看由DesignsbyZephyr制作的有关网络实现的这些教程,但出现此错误:
java.net.BindException: Address already in use: Cannot bind
at java.net.DualStackPlainDatagramSocketImpl.socketBind(Native Method)
at java.net.DualStackPlainDatagramSocketImpl.bind0(Unknown Source)
at java.net.AbstractPlainDatagramSocketImpl.bind(Unknown Source)
at java.net.DatagramSocket.bind(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at java.net.DatagramSocket.<init>(Unknown Source)
at com.tdd12.eotu.net.GameServer.<init>(GameServer.java:22)
at com.tdd12.eotu.Game.<init>(Game.java:39)
at com.tdd12.eotu.Game.main(Game.java:121)
Exception in thread "Thread-3" java.lang.NullPointerException
at com.tdd12.eotu.net.GameServer.run(GameServer.java:37)
When I start the game two times with the same port. 当我使用相同的端口两次启动游戏时。 That sounds pretty weird, doesn't it?
听起来很奇怪,不是吗?
I don't know why, maybe because I'm not very experienced with network programming (as you've maybe already understood). 我不知道为什么,也许是因为我对网络编程没有很丰富的经验(正如您可能已经了解的那样)。
Here is the code I'm using: 这是我正在使用的代码:
(The code is placed in classes and packages, so they are correctly formatted. I just didn't write that here) (代码放置在类和包中,因此它们的格式正确。我只是在这里没有写出来)
GameServer.java: GameServer.java:
// The socket
private DatagramSocket socket;
// The main game
private Game game;
// The constructor
public GameServer(Game game) {
// Assign variables
this.game = game;
try {
this.socket = new DatagramSocket(9527);
} catch (SocketException e) {
e.printStackTrace();
}
}
// Run the thread
public void run() {
while(true) {
// The data to include in the packet (data to send)
byte[] data = new byte[1024];
// The packet to send
DatagramPacket packet = new DatagramPacket(data, data.length);
// Recieve data from the server
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
// Get the message
String message = new String(packet.getData());
// Print the message
System.out.println("CLIENT [" + packet.getAddress().getHostAddress() + ":" + packet.getPort() + "] > " + new String(packet.getData()));
// If the message is equal to "ping"
if(message.trim().equalsIgnoreCase("ping")) {
// Send back a message with the text "pong"
sendData("pong".getBytes(), packet.getAddress(), packet.getPort());
}
}
}
// Send data to the server
public void sendData(byte[] data, InetAddress ipAddress, int port) {
// Create a new packet with the inputed data
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port);
// Send the packet to the server
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
GameClient.java: GameClient.java:
// The IP address
private InetAddress ipAddress;
// The socket
private DatagramSocket socket;
// The main game
private Game game;
// The constructor
public GameClient(Game game, String ipAddress) {
// Assign variables
this.game = game;
try {
this.socket = new DatagramSocket();
this.ipAddress = InetAddress.getByName(ipAddress);
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
}
}
// Run the thread
public void run() {
while(true) {
// The data to include in the packet (data to send)
byte[] data = new byte[1024];
// The packet to send
DatagramPacket packet = new DatagramPacket(data, data.length);
// Recieve data from the server
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
// Print the data
System.out.println("SERVER > " + new String(packet.getData()));
}
}
// Send data to the server
public void sendData(byte[] data) {
// Create a new packet with the inputed data
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 9527);
// Send the packet to the server
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
I'd greatly appreciate if someone could help me with this. 如果有人可以帮助我,我将不胜感激。 Thanks!
谢谢!
As Diptopol Dam said, calling the 正如Diptopol大坝所说,
DatagramSocket.close();
method before the application is closed fixed the problem. 应用程序关闭前的方法解决了该问题。 Thanks Diptopol Dam!
感谢Diptopol大坝!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.