2016-06-02 25 views
0

これは私をナットにしています。 Buffered Imageを含むオブジェクトとサーバクライアントシステムを1秒間に60回更新してレンダリングしてみると、私は完全に失われてしまいました。私はすべてのことを正しくやっていると思ったので、私はサーバークライアントシステムを切り離し、ソケットでオブジェクトを送信するように見えませんでした。オブジェクトがソケットを介して送信されていません

オブジェクトを受信しようとすると、プログラムが停止して進行していないように見えます。

コードがオンにキャッチされた特定の場所は次のとおりです。

Troops troop2 = (Troops)c.receiveObject(); 

System.out.printさんによって推定されます。残りのコードは以下の通りです。エラーはスローされません.IterfacerとInterfacer2を実行します。ここ

public class Interfacer{ 
public static void main(String[] args){ 
    try { 
     Client c = new Client(); 
     System.out.println("it ran 1/2...client"); 
     while(true){ 
      Troops troop2 = (Troops)c.receiveObject(); 
      JOptionPane.showMessageDialog(null, troop2.getX()); 
      System.out.println("it ran...client"); 
     } 
    } catch (IOException | ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} 



} 





public class Interfacer2{ 
public static void main(String[] args){ 
    try { 
     Server s = new Server(); 
     Troops troop = new Goblin(1,1,1); 
     s.sendObject(troop); 
     System.out.println("it ran...server"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
} 

public class Client extends JFrame{ 


private static final long serialVersionUID = 1L; 
private ObjectOutputStream output; 
private ObjectInputStream input; 

private String serverIP; 
private Socket connection; 

JTextArea t; 
JFrame f; 

//constructor 
public Client(String host){ 

    serverIP = host; 

    f = new JFrame(); 
    f.getContentPane().setPreferredSize(new Dimension(300, 300)); 
    f.pack(); 

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); 
    t = new JTextArea(); 
    f.add(t, BorderLayout.CENTER); 
    f.setVisible(true); 

    try{ 
     connectToServer(); 
     setupStreams(); 
    }catch(EOFException eofException){ 
     //t.append("Connection was terminated"); 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 

    public Client(){ 

    serverIP = "127.0.0.1"; 

    f = new JFrame(); 
    f.getContentPane().setPreferredSize(new Dimension(300, 300)); 
    f.pack(); 

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); 
    t = new JTextArea(); 
    f.add(t, BorderLayout.CENTER); 
    f.setVisible(true); 


    try{ 
     connectToServer(); 
     setupStreams(); 
    }catch(EOFException eofException){ 
     //t.append("Connection was terminated"); 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 



//connect to server 
private void connectToServer() throws IOException{ 
    t.append("Attempting connection..."); 
    connection = new Socket(InetAddress.getByName(serverIP), 7382); 
    t.append("Connection Established! Connected to: " + connection.getInetAddress().getHostName()); 
} 

//set up streams 
private void setupStreams() throws IOException{ 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
    t.append(" The streams are now set up!"); 
} 


//Close connection 
public void closeConnection(){ 
    //t.append(" Closing the connection!"); 
    try{ 
     output.close(); 
     input.close(); 
     connection.close(); 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 

public void sendObject(Object o) throws IOException{ 
    output.writeObject(o); 
    output.flush(); 
} 
public Object receiveObject() throws IOException, ClassNotFoundException{ 
    return input.readObject(); 
} 

} 


public class Server{ 



private ObjectOutputStream output; 
private ObjectInputStream input; 
private ServerSocket server; 
private Socket connection; 

JTextArea t; 
JFrame f; 

//constructor 
public Server(){ 


    f = new JFrame(); 
    f.getContentPane().setPreferredSize(new Dimension(300, 300)); 
    f.pack(); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.setLocationRelativeTo(null); 
    t = new JTextArea(); 
    f.add(t, BorderLayout.CENTER); 
    f.setVisible(true); 

    try{ 
     server = new ServerSocket(7382, 100); //6789 is a dummy port for testing, this can be changed. The 100 is the maximum people waiting to connect. 
     while(true){ 
      try{ 
       //Trying to connect and have conversation 
       waitForConnection(); 
       setupStreams(); 
      }catch(EOFException eofException){ 
       //t.append("Connection was terminated"); 
      } 
     } 
    } catch (IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 

//wait for connection, then display connection information 
private void waitForConnection() throws IOException{ 
    t.append(" Waiting for someone to connect..."); 
    connection = server.accept(); 
    t.append(" Now connected to " + connection.getInetAddress().getHostName()); 
} 

//get stream to send and receive data 
private void setupStreams() throws IOException{ 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 

    input = new ObjectInputStream(connection.getInputStream()); 

    t.append(" Streams are now setup "); 
} 


     // input.readObject(); 


public void closeConnection(){ 
    //t.append(" Closing Connections... "); 
    try{ 
     output.close(); //Closes the output path to the client 
     input.close(); //Closes the input path to the server, from the client. 
     connection.close(); //Closes the connection between you can the client 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 
public void sendObject(Object o) throws IOException{ 
    output.writeObject(o); 
    output.flush(); 
} 
public Object receiveObject() throws IOException, ClassNotFoundException{ 
    return input.readObject(); 
} 

} 


public class Troops implements Serializable{ 
private int x; 
private int y; 


private int health; 
private int movSpeed; 
private int movSpeedx; 
private int movSpeedy; 
private int cost; 
private long deployCoolDown; 
private int level; 
private transient BufferedImage image; 
private int size = 16; 
/** 
* 
* @param x 
* @param y 
* @param level 
* @param health 
* @param movSpeed 
* @param movSpeedx 
* @param movSpeedy 
* @param cost 
* @param deployCoolDown 
* @param image 
* 
*/ 

public Troops(int x, int y, int level, int health, int movSpeed, int movSpeedx, int movSpeedy, int cost, long deployCoolDown, BufferedImage image){ 
    this.x = x; 
    this.y = y; 
    this.level = level; 
    this.health = health; 
    this.movSpeed = movSpeed; 
    this.movSpeedx = -movSpeed; 
    this.movSpeedy = movSpeedy; 
    this.cost = cost; 
    this.deployCoolDown = deployCoolDown; 
    this.image = image; 

} 

public int getX() { 
    return x; 
} 
public void setX(int x) { 
    this.x = x; 
} 
public int getY() { 
    return y; 
} 
public void setY(int y) { 
    this.y = y; 
} 
public int getSize(){ 
    return size; 
} 
public int getMovSpeed() { 
    return movSpeed; 
} 
public void setMovSpeed(int movSpeed) { 
    this.movSpeed = movSpeed; 
} 
public int getHealth() { 
    return health; 
} 

public void changeHealth(int health) { 
    this.health += health; 
} 

public int getCost() { 
    return cost; 
} 

public void setCost(int cost) { 
    this.cost = cost; 
} 

public long getDeployCoolDown() { 
    return deployCoolDown; 
} 

public void setDeployCoolDown(int deployCoolDown) { 
    this.deployCoolDown = deployCoolDown; 
} 

private void writeObject(ObjectOutputStream out)throws IOException{ 
    out.defaultWriteObject(); 
    //write buff with imageIO to out 
} 

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ 
    in.defaultReadObject(); 
    //read buff with imageIO from in 
} 


} 

私は軍隊の階級の過渡とreadObject/writeObjectメソッドの部分を得た場所である:

Java - Sending an object that points to a BufferedImage through a Socket

+1

'Troop'のコードを表示してください。定義上、 'transient'フィールドは直列化されません。 –

+0

「送信できませんでした」は問題の記述ではありません。あなたが何を求めているのか不明です。あなたが投稿した 'writeObject()'と 'readObject()'メソッドは意味がありません。削除してください。デフォルトではまったく同じことが起こります。 – EJP

+0

質問を更新し、wrtite/readObject()メソッドが見つかった場所にリンクを張りました。私はより良い問題の説明を与えるだろうが、エラーがスローされていないと私は十分なソケットが問題がある理由を把握するために知りません。また、文字列などのよりシンプルなオブジェクトで送信しようとしましたが、同じ結果が得られました。 – Deek3117

答えて

0

ServerのコンストラクタはIOExceptionまで終了しませんwhileループが含まれていますEOFException以外のものがキャッチされているため、Interfacer2クラスは、s.sendObject()という次の行に到達することはありません。

悪いデザイン。コンストラクターは無限ループやネットワークI/Oに関与せずにオブジェクトを構築する必要があります。

+0

それは問題でした。私はインスタントメッセンジャーのチュートリアルからこのコードを使用していました。クライアントのwhileループを削除して、サーバーでそれを行うのを忘れてしまったと思います。私は本当に助けに感謝します – Deek3117

関連する問題