画图应用无法发送到本地服务器

Paint App Cannot Send to Local Server

我正在为 CS class 编写一个画图应用程序,但我无法让它向服务器发送数据。它应该连接到服务器,以便多个客户端可以一起处理同一幅画,但我似乎无法将新对象发送到服务器。我知道它连接到服务器,因为它在建立连接但 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:”目标是在释放鼠标时通知并发送到服务器。

---- [已解决] ----

我遇到了一些问题:

首先:我在与上面的代码相同 class 但我没有在构造函数中创建它的实例。哑的。我知道。

其次:我发送到服务器的对象 (PaintObject) 不可序列化。 对象必须是可序列化的才能使用 ObjectOutputStream.writeObject 发送。 public PaintObject class 现在开始:

public class PaintObject implements Serializable {

这些就是问题所在。我只是想我会成为一名优秀的互联网公民,并在我破解它时回答我自己的问题。我希望这对未来有所帮助。