2014-11-02 16 views
0

我正在創建一個LWJGL策略遊戲,並且我正在實現多人遊戲。Java網絡 - java.net.BindException

現在遊戲只是生成一些不同的瓷磚類型的世界。

我想我現在應該開始實現網絡連接,讓服務器生成世界,所有客戶加入下載世界並加載它(儘管遊戲幾乎不可玩),以便更容易地實現更高級的以後的東西。現在解決問題!

我對網絡實施,由DesignsbyZephyr提出看these教程,但我得到這個錯誤:

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

如果有人能幫助我,我將不勝感激。 謝謝!

+1

我覺得端口沒有正常關閉。因此,當服務器第二次嘗試偵聽端口(它已被前一次運行佔用)正在獲取此錯誤。 – 2014-11-02 11:07:15

+0

好的,你知道如何關閉端口嗎? – 2014-11-02 11:09:56

+0

我認爲datagramSocket.close會做的。 http://download.java.net/jdk7/archive/b123/docs/api/java/net/DatagramSocket.html#close%28%29 – 2014-11-02 11:13:16

回答

0

正如Diptopol壩說,調用

DatagramSocket.close(); 

方法之前應用是閉固定的問題。謝謝Diptopol Dam!