2016-03-27 8 views
0

私はCSクラスのペイントアプリケーションを作成しており、サーバにデータを送信することができません。複数のクライアントが同じ絵を一緒に扱うことができるようにサーバーに接続することになっていますが、新しいオブジェクトをサーバーに送信できないようです。接続が確立されてもObjectOutputStream.writeObjectがサーバーに到達できないときに両端でフィードバックを返すため、サーバーに接続することがわかります。私に何が足りないのか教えてください!みんな、ありがとう!ペイントアプリケーションがローカルサーバに送信できません

private ArrayList<PaintObject> shapes = new ArrayList<PaintObject>(); 
private Point startDrag, endDrag; 
private ColorShapeSelectorJPanel colorShapeChooserArea = new ColorShapeSelectorJPanel(); 
private PaintObject currPaintObj = null; 
private Socket socket; 
private ObjectOutputStream oos; 
private ObjectInputStream ois; 

private static final String ADDRESS = "localhost"; 

public PaintingField() { 
    this.setBackground(Color.WHITE); 
    this.setSize(2000, 1400); 
    this.openConnection(); 
    initializeListeners(); 

} 

// Establish connection with the server. 
private void openConnection() { 
    /* Our server is on our computer, but make sure to use the same port. */ 
    try { 
     // Connect to the Server 
     socket = new Socket(ADDRESS, Server.SERVER_PORT); 
     oos = new ObjectOutputStream(socket.getOutputStream()); 
     ois = new ObjectInputStream(socket.getInputStream()); 
     System.out.println("Connected to server at " + ADDRESS + ":" + Server.SERVER_PORT); 
    } catch (IOException e) { 
     // e.printStackTrace(); 
     this.cleanUpAndQuit("Couldn't connect to the server"); 
    } 

} 

// Remove connection with server 
private void cleanUpAndQuit(String string) { 
    JOptionPane.showMessageDialog(PaintingField.this, string); 
    try { 
     if (socket != null) 
      socket.close(); 
    } catch (IOException e) { 
     // Couldn't close the socket, we are in deep trouble. Abandon ship. 
     e.printStackTrace(); 

    } 
} 

// Get listeners running 
private void initializeListeners() { 
    this.addMouseListener(new MouseAdapter() { 
     // Begin dragging the shape 
     public void mousePressed(MouseEvent evnt) { 
      startDrag = new Point(evnt.getX(), evnt.getY()); 
      endDrag = startDrag; 
      repaint(); 
     } 

     // When mouse is released, get the shape 
     @Override 
     public void mouseReleased(MouseEvent evnt) { 

      // If rectangle... 
      if (colorShapeChooserArea.isRectangleSelected()) { 
       currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // If ellipse... 
      else if (colorShapeChooserArea.isEllipseSelected()) { 
       currPaintObj = new PaintObject(makeEllipse(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // if line 
      else if (colorShapeChooserArea.isLineSelected()) { 
       currPaintObj = new PaintObject(makeLine(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         colorShapeChooserArea.getColor(), false); 
      } 

      // if doge 
      else if (colorShapeChooserArea.isImageSelected()) { 
       currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()), 
         Color.WHITE, true); 
      } 

      // Send the object to the server 
      // TODO: FIXME: oos.writeObject NOT SENDING!!! 
      try { 
       /* Someone pressed enter? Send the message to the server! */ 
       oos.writeObject(currPaintObj); 
      } catch (IOException ex) { 
       PaintingField.this.cleanUpAndQuit("Couldn't send a message to the server"); 
      } 

      shapes.add(currPaintObj); 
      startDrag = null; 
      endDrag = null; 
      repaint(); 

     } 

    }); 

    this.addMouseMotionListener(new MouseMotionAdapter() { 
     public void mouseDragged(MouseEvent evnt) { 
      endDrag = new Point(evnt.getX(), evnt.getY()); 
      repaint(); 
     } 
    }); 

} 

問題行は「// TODO:FIXME:」マークされている目標は通知し、マウスボタンを離したときにサーバーに送信することでした。

答えて

1

---- [SOLVED] ----

を私はいくつかの問題だった:まず

を:私はこれと同じクラス内プライベートServerListenerクラスを作成していました上のコードは、私はコンストラクタでそのインスタンスを作成していないからです。ダム。知っている。第二に

:私は、サーバー(PaintObject)にオーバー送っていたオブジェクトはシリアライズませんでした。 オブジェクトは、ObjectOutputStream.writeObjectを使用して送信するためにはシリアル化可能でなければなりません。公開されるPaintObjectクラスは、次のように始まります。

public class PaintObject implements Serializable { 

これらは問題でした。私はちょうど私が良いインターネット市民になると思って、私はそれをクラックしたときに私自身の質問に答えます。私はこれが将来的にいくらか助けてくれることを願っている

関連する問題