0

我正在尝试将 ArrayList 从服务器发送到客户端,但它不起作用。服务器确实发送整数。如果客户端发送列表,它也可以工作。

这是我尝试发送的对象(仅字段)

public class DrawingPoint implements Serializable
{
    private double x;
    private double y;
    private boolean paint;
    Color c;
    private int dikte;
    boolean gum;    

    public DrawingPoint(double x, double y, boolean paint, Color c, int dikte,
            boolean gum) {
        super();
        this.x = x;
        this.y = y;
        this.paint = paint;
        this.c = c;
        this.dikte = dikte;
        this.gum = gum;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public boolean isPaint() {
        return paint;
    }

    public int getDikte() {
        return dikte;
    }
//getters and setters

}

这是服务器和客户端的代码

服务器(这不是服务器,但这个类接收和发送东西。服务器用这些对象创建一个数组,然后让它发送。)

package MultiplayerPaint.socket.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.Socket;
import java.util.ArrayList;

import MultiplayerPaint.DrawingPoint;

public class ThreadClass extends Thread implements Runnable, Serializable{

    transient Socket socket;
    transient Server server;
    private transient ObjectInputStream inputFromClient;
    private transient ObjectOutputStream outputToClient;
    public ArrayList<DrawingPoint> list = new ArrayList<>();
    String name;
    public int nummer;
    transient private boolean changed = false;

    public ThreadClass(Socket socket, Server server, int nummer)
    {
        this.server = server;
        this.nummer = nummer;
        try {
            inputFromClient = new ObjectInputStream(socket.getInputStream());
            outputToClient = new ObjectOutputStream(socket.getOutputStream());
            runOnce();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while(true)
        {
            try {
                Thread.sleep(1000/5);
                ArrayList<DrawingPoint> l = (ArrayList<DrawingPoint>) inputFromClient.readObject();
                list = l;
                changed = true;
            } catch (Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
    }

    public void runOnce()
    {
        try {
            outputToClient.writeInt(nummer);
            outputToClient.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean isChanged() {
        return changed;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    public void sending(ThreadClass[] sturen) {
        try {
            for(ThreadClass t : sturen)
            {
                outputToClient.writeObject(t);
            }
            outputToClient.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是客户端

package MultiplayerPaint.socket;

import java.awt.Color;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import MultiplayerPaint.DrawingPoint;
import MultiplayerPaint.PaintModel;
import MultiplayerPaint.socket.server.Server;
import MultiplayerPaint.socket.server.ThreadClass;

public class Client
{

    private ObjectInputStream inputFromClient;
    private ObjectOutputStream outputToClient;
    int aantal= -1;
    int nummer;

    public Client(final PaintModel m)
    {
        try {
            Socket socket = new Socket(Server.HOST, Server.PORT);
            outputToClient = new ObjectOutputStream(socket.getOutputStream());
            inputFromClient = new ObjectInputStream(socket.getInputStream());
            nummer = inputFromClient.readInt();
            m.nummer = nummer;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        Thread sturen = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while(true)
                    {

                        ArrayList<DrawingPoint> l = new ArrayList<>();
                        l.addAll(m.getPoints());
                        outputToClient.writeObject(l);
                        outputToClient.flush();
                        aantal = m.getPoints().size();
                        Thread.sleep(1000/5);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        sturen.start();
        Thread ontvangen = new Thread(new Runnable() {

            @Override
            public void run() {
                while(true)
                {
                    try {
                        ArrayList<ThreadClass> l = new ArrayList<>();
                        for(int i = 0; i< 4; i++)
                        {
                            l.add((ThreadClass) inputFromClient.readObject());
                        }
                        Iterator<ThreadClass> it = l.iterator();
                        while(it.hasNext())
                        {
                            ThreadClass t = it.next();
                            if(t.nummer == nummer) 
                            {
                                System.out.println(t.nummer + " " + t.list.size());
                                for(DrawingPoint p: t.list)
                                {
                                    if(p == null) System.out.println("null");
                                    else System.out.println(t.nummer + " X " + p.getX() + " Y " + p.getY());
                                }
                                continue;
                            }
                            System.out.println(t.nummer + " " + t.list.size());

                            m.otherPoints.put(t.nummer, t.list);
                        Thread.sleep(1000/5);
                        }
                    }
                    catch (Exception e1)
                    {
                        e1.printStackTrace();
                    }
                }
            }
        });
        ontvangen.start();
    }
}
4

1 回答 1

0

要通过套接字发送对象,我建议您先序列化对象并发送它们,然后在收到对象时将它们反序列化。

例如,我将对象转换为 JSON 格式并将其保存在字符串中,将字符串发送到服务器,服务器将其反序列化为 java 对象,然后使用序列化对象再次回答,依此类推。

//客户端
人 人;
String stringJSON = serializer.serialize(person);

socket.getOutputStream().println(stringJSON);

//服务器端

String stringJSON = socket.getInputStream().readLine();
人 person = JSONDeserializer.deserialize(stringJSON)

我发现这种方式更容易,如果有兴趣我用它来序列化/反序列化http://flexjson.sourceforge.net/

顺便说一句,您必须为您的对象提供 setter 和 getter。

于 2013-06-03T13:25:06.450 回答