2016-04-23 11 views
0

私はクライアントとサーバをプロジェクトに設定していますが、ネットワーク上のコンピュータ間で接続していません。UDP接続がlanで動作しない

クライアント:

public class GameClient extends Thread { 

private static Image image; 

private InetAddress ipAddress; 
private DatagramSocket socket; 
private Play play; 

public GameClient(Play play, String ipAddress){ 
    this.play = play; 

    try { 
     this.socket = new DatagramSocket(); 
     this.ipAddress = InetAddress.getByName(ipAddress); 
    } catch (SocketException e) { 
     e.printStackTrace(); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 

} 

public GameClient(Play play, InetAddress ipAddress){ 
    this.play = play; 

    try { 
     this.socket = new DatagramSocket(); 
     this.ipAddress = ipAddress; 
    } catch (SocketException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

public void run() { 
    //boolean run = true; 
    while(true) { 
     byte[] data = new byte[1024]; 
     DatagramPacket packet = new DatagramPacket(data, data.length); 
     try { 
      socket.receive(packet); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort()); 
    } 
} 

public static void getImage(Image newImage){ 
    image = newImage; 
} 

private void parsePacket(byte[] data, InetAddress address, int port) { 
    String message = new String(data).trim(); 
    PacketTypes type = Packet.lookupPacket(message.substring(0, 2)); 
    Packet packet = null; 
    switch (type) { 
    default: 
    case INVALID: 
     break; 
    case LOGIN: 
     packet = new Packet00Login(data); 
     System.out.println("[" + address.getHostAddress() + ":" + port + "] " + ((Packet00Login)packet).getUsername() + " has joined the game"); 
     PlayerMP player = new PlayerMP(((Packet00Login)packet).getUsername(), (double)200, (double)200, 65, 285, (float)0, (double)1, image, address, port); 

     play.addEntity(player); 
     play.setClientPlayer(player); 
     break; 
    case DISCONNECT: 
     packet = new Packet01Disconnect(data); 
     System.out.println("[" + address.getHostAddress() + ":" + port + "] " + ((Packet01Disconnect)packet).getUsername() + " has left the world..."); 
     play.removePlayerMP(((Packet01Disconnect)packet).getUsername()); 
     break; 
    case MOVE: 
     packet = new Packet02Move(data); 
     handleMove((Packet02Move) packet); 

    } 
} 

public void sendData(byte[] data){ 
    DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 1331); 
    try { 
     socket.send(packet); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

private void handleMove(Packet02Move packet){ 
    this.play.movePlayer(packet.getUsername(), packet.getX(), packet.getY(), packet.getDirection()); 
} 

} 

サーバー:それは私の演劇クラスからサーバーへの最初のログイン・パケットを送信した場合、ここで

public class GameServer extends Thread { 

private static Image image; 

private DatagramSocket socket; 
private Play play; 
private List<PlayerMP> connectedPlayers = new ArrayList<PlayerMP>(); 

public GameServer(Play play){ 
    this.play = play; 

    try { 
     this.socket = new DatagramSocket(1331); 
    } catch (SocketException e) { 
     e.printStackTrace(); 
    } 

} 

public static void getImage(Image newImage){ 
    image = newImage; 
} 

public void run() { 
    while(true) { 
     byte[] data = new byte[1024]; 
     DatagramPacket packet = new DatagramPacket(data, data.length); 
     try { 
      socket.receive(packet); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     this.parsePacket(packet.getData(), packet.getAddress(), packet.getPort()); 
    } 
} 

private void parsePacket(byte[] data, InetAddress address, int port) { 
    String message = new String(data).trim(); 
    PacketTypes type = Packet.lookupPacket(message.substring(0, 2)); 
    Packet packet = null; 
    switch (type) { 
    default: 
    case INVALID: 
     break; 
    case LOGIN: 
     packet = new Packet00Login(data); 
     System.out.println("[" + address.getHostAddress() + ":" + port + "] " + ((Packet00Login)packet).getUsername() + " has connected to the server..."); 
     PlayerMP player = new PlayerMP(((Packet00Login)packet).getUsername(), (double)200, (double)200, 65, 285, (float)0, (double)1, image, address, port); 
     this.addConnection(player, (Packet00Login)packet); 

     break; 
    case DISCONNECT: 
     packet = new Packet01Disconnect(data); 
     System.out.println("[" + address.getHostAddress() + ":" + port + "] " + ((Packet01Disconnect)packet).getUsername() + " has left..."); 
     this.removeConnection((Packet01Disconnect)packet); 
     break; 
    case MOVE: 
     packet = new Packet02Move(data); 
     //System.out.println(((Packet02Move)packet).getUsername() + " has moved to " + (int)((Packet02Move)packet).getX() + " , " + (int)((Packet02Move)packet).getY()); 
     this.handleMove(((Packet02Move)packet)); 
     break; 
    } 
} 

public void addConnection(PlayerMP player, Packet00Login packet) { 
    boolean alreadyConnected = false; 
    if(!this.connectedPlayers.isEmpty()){ 
     for(PlayerMP p: this.connectedPlayers){ 
      if(player.getUsername().equalsIgnoreCase(p.getUsername())){ 
       System.out.println("Client " + player.getUsername() + " already conected as " + p.getUsername() + ": updating player information"); 
       if(p.getIpAddress() == null){ 
        p.setIpAddress(player.getIpAddress()); 
       } 
       if(p.getPort() == -1){ 
        p.setPort(player.getPort()); 
       } 
       alreadyConnected = true; 
      }else{ 
       // relay to the current connected player that there is a new 
       // player 
       sendData(packet.getData(), p.getIpAddress(), p.getPort()); 

       // relay to the new player that the currently connect player 
       // exists 

       packet = new Packet00Login(p.getUsername()); 
       System.out.println("Sending already conected player to [" + player.getIpAddress().getHostAddress() + ":" + player.getPort() + "] " + player.getUsername()); 
       sendData(packet.getData(), player.getIpAddress(), player.getPort()); 
      } 
     } 
    }else{ 
     System.out.println("Sending login to [" + player.getIpAddress().getHostAddress() + ":" + player.getPort() + "] " + player.getUsername()); 
     sendData(packet.getData(), player.getIpAddress(), player.getPort()); 
     this.connectedPlayers.add(player); 
     alreadyConnected = true; 
    } 
    if(!alreadyConnected){ 
     System.out.println("Sending login to [" + player.getIpAddress().getHostAddress() + ":" + player.getPort() + "] " + player.getUsername()); 
     packet = new Packet00Login(player.getUsername()); 
     sendData(packet.getData(), player.getIpAddress(), player.getPort()); 
     this.connectedPlayers.add(player); 

    } 
} 


public void removeConnection(Packet01Disconnect packet) { 
    this.connectedPlayers.remove(getPlayerMPIndex(packet.getUsername())); 
    packet.writeData(this); 
} 

public PlayerMP getPlayerMP(String username){ 
    for(PlayerMP player : this.connectedPlayers){ 
     return player; 
    } 
    return null; 
} 

public int getPlayerMPIndex(String username){ 
    int index = 0; 
    for(PlayerMP player : this.connectedPlayers){ 
     if(player.getUsername().equals(username)){ 
      break; 
     } 
     index++; 
    } 
    return index; 
} 

public void sendData(byte[] data, InetAddress ipAddress, int port) { 
    DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port); 
    try { 
     this.socket.send(packet); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void sendDataToAllClients(byte[] data) { 
    for (PlayerMP p : connectedPlayers) { 
     sendData(data, p.getIpAddress(), p.getPort()); 
    } 
} 

private void handleMove(Packet02Move packet){ 
    if(getPlayerMP(packet.getUsername()) != null){ 
     int index = getPlayerMPIndex(packet.getUsername()); 
     this.connectedPlayers.get(index).x = packet.getX(); 
     this.connectedPlayers.get(index).y = packet.getY(); 
     ((PlayerMP)this.connectedPlayers.get(index)).playerDirection = packet.getDirection(); 
     packet.writeData(this); 
    } 
} 

} 

がある(このパケットを受信して​​いない)

if(JOptionPane.showConfirmDialog(null, this, "do you want to start the server?", mousePosX) == 0){ 
     socketServer = new GameServer(this); 
     socketServer.start(); 
    } 
    try { 
     socketClient = new GameClient(this, InetAddress.getLocalHost()); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } 
    socketClient.start(); 
    GameClient.getImage(standing);// these are just so that the client and the server have the image to create the player entity once they join the game 

    GameServer.getImage(standing); 
    Packet00Login loginPacket = new Packet00Login(Global.playerUsername); 
    loginPacket.writeData(socketClient); //this is where the first login is being sent, it is not being received. 

明らかに、このプログラムは1台のコンピュータで動作し、ipの外観は正しい(192.0.0.XXX)しかし、私が1台のコンピュータでサーバを稼働させて、もう1台のコンピュータでサーバを稼動させないようにすると、2台目のコンピュータは最初のコンピュータに接続しません。

ありがとうございます!

+1

PCファイアウォールを無効にしようとしました –

+0

@PeterLawrey他のアイデアはありませんでしたか? – Canyonman133

+0

サーバーとクライアントをどのように設定していますか? –

答えて

0

ネットワークをうまくやってくれることは何でもありました。Pは、私が誤ってクライアントではなく情報を送信するためにサーバーを直接呼び出すことが判明しました。私は最初にやっていたはずだったように、サーバーを新しいプロジェクトに移動し、スタンドアロンとしてセットアップしました。しかし、助けてくれてありがとう!

関連する問題