简体   繁体   中英

Sending selfmade objects with Java sockets

I'm trying to send an ArrayList from a server to a client, but it doesn't work. The server does send ints. If the client send the list, it works too.

Here is the object i try to send (only the fields)

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

}

Here is the code for the server and client

server (this isn't the server but this class recieves and sends stuff. The server makes an array with these object and it let's it send.)

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();
        }
    }
}

This is the client

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();
    }
}

To send objects over sockets I would reckommend you to serialize your objects first and send them, after that when your objects are received they are deserialized.

For example I convert my objects to JSON format and save it in a string, send the string to server, the server deserializes it to a java object then answers again with a serialized object and so on.

//Client side
Person person;
String stringJSON = serializer.serialize( person );

socket.getOutputStream().println(stringJSON);

//Server Side

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

I find this way much easier, if intrested I use this to serialize/desrialize http://flexjson.sourceforge.net/

By the way you have to provide setters and getters for your objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM