【问题标题】:Java networking - java.net.BindExceptionJava 网络 - java.net.BindException
【发布时间】:2015-04-30 06:00:49
【问题描述】:

我正在创建一个 LWJGL 策略游戏,并在其中实现多人游戏。

现在游戏只是在生成一个包含一些不同瓷砖类型的世界。

我想我现在应该开始实现网络,让服务器生成世界,所有加入的客户端下载并加载这个世界(即使游戏还勉强可玩),以便更容易实现更高级的东西稍后的。现在问题来了!

我正在观看these 的网络实现教程,由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)

当我用同一个端口启动游戏两次时。这听起来很奇怪,不是吗?

我不知道为什么,可能是因为我对网络编程不是很有经验(你可能已经理解了)。

这是我正在使用的代码:
(代码放在类和包中,所以格式是正确的。只是这里没有写)

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:

// 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();
    }
}

如果有人能帮我解决这个问题,我将不胜感激。 谢谢!

【问题讨论】:

  • 我认为端口没有正确关闭。因此,当服务器第二次尝试侦听端口(已被上一次运行占用)时出现此错误。
  • 好的,你知道如何再次关闭端口了吗?
  • 我认为 datagramSocket.close 可以解决问题。 download.java.net/jdk7/archive/b123/docs/api/java/net/…
  • 好的,我试试。我现在遇到了一些问题,我会解决它们,然后返回答案。

标签: java network-programming lwjgl


【解决方案1】:

正如 Diptopol Dam 所说,调用

DatagramSocket.close();  

应用程序关闭前的方法修复了问题。感谢 Diptopol 大坝!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多