我正在尝试将 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();
}
}