当我想发送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