java - 画图应用程序无法发送到本地服务器

标签 java server client objectoutputstream

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

最佳答案

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

我遇到了一些问题:

首先:我在上面的代码来自的同一个类中创建了一个私有(private)ServerListener,但我没有创建一个它在构造函数中的实例。哑的。我知道。

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

public class PaintObject implements Serializable {

这些就是问题所在。我只是想当我破解它时我会成为一个好的互联网公民并回答我自己的问题。我希望这对 future 有所帮助。

关于java - 画图应用程序无法发送到本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252868/

相关文章:

java - mysql UUID() 和 java UUID.randomUUID()

java - 破解 Log4j 以发送自定义 SysLog 消息

Apache:按 session 进行负载平衡

servlets - 如何在servlet中获取客户端的远程地址?

perl - 使用 Event::Lib 制作 perl 聊天客户端

java - 为什么 Java 需要 EnumClass.VALUE 而不仅仅是 VALUE?

android - 连接2个蓝牙设备

java-nio : send from different thread than selecting

c# - 使用 SSL 的套接字身份验证

java - 从输入表单检索数据并发出 get 请求 spring mvc