2016-03-27 108 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

---- [解決] ----

我有幾個問題:

首先:我創造了同級別內的私人ServerListener類的上面的代碼是來自但我沒有在構造函數中創建它的一個實例。啞。我知道。

其次:我發送到服務器的對象(PaintObject)是不可序列化的。 對象必須是可序列化的才能使用ObjectOutputStream.writeObject發送。公共PaintObject類現在開始:

public class PaintObject implements Serializable { 

這些是問題。我只是認爲我會成爲一名優秀的互聯網公民,並在我解決問題時回答我自己的問題。我希望這有助於未來。

相關問題