通过套接字java发送颜色对象

时间:2016-06-23 15:51:48

标签: java networking serversocket

当我想发送Color对象ovear套接字时,方法writeObject不起作用。 ...

public class Server {
public static void main(String[] args) throws IOException, InterruptedException {
    ServerSocket listener = new ServerSocket(9090);
    while (true) {
        Socket socket = listener.accept();
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        out.writeObject(Color.red);
        out.writeObject(Color.red);
        out.writeObject(Color.red);
    }
}
}

...

public class Client {
public static void main(String[] args) throws IOException, ClassNotFoundException {
    Socket socket = new Socket("", 9090);
    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    System.out.println(in.readObject());
    System.out.println(Color.red == in.readObject());
    System.out.println(new Color(255, 0, 0) == in.readObject());
}
}

输出:

java.awt.Color[r=255,g=0,b=0]
false //the output must be true
false //thre output must be true

0 个答案:

没有答案