2016-03-27 103 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 { 

这些是问题。我只是认为我会成为一名优秀的互联网公民,并在我解决问题时回答我自己的问题。我希望这有助于未来。

相关问题