简体   繁体   中英

Java networking - java.net.BindException

I'm creating a LWJGL Strategy Game, and I'm implementing multiplayer into it.

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:

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:

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

I'd greatly appreciate if someone could help me with this. Thanks!

As Diptopol Dam said, calling the

DatagramSocket.close();  

method before the application is closed fixed the problem. Thanks Diptopol Dam!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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