当我想使用套接字发送对象时的Java异常

时间:2014-03-15 23:30:21

标签: java sockets objectinputstream objectoutputstream

我想制作服务器客户端应用程序,其中服务器将等待对象,当服务器获取对象时,然后使用它。但是当服务器想要获取对象时我遇到了问题。

服务器:

public static void main(String[] argv) throws IOException {
    String command;
    List<Game> games = new ArrayList<>();
    List<Player> players = new ArrayList<>();

    serverSocket = new ServerSocket(9999);

    while(true){
        System.out.println("Waiting for player");
        Socket socket = serverSocket.accept();
        System.out.println("Player connected, waiting for command");

        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        System.out.println("Output created");

        Player p = new Player(out);
        players.add(p);
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        System.out.println("input created");
        ServerReceiver receiver = new ServerReceiver(games, players, in);
        receiver.start();

    }
}

public class ServerReceiver extends Thread{

private List<Game> games;
private List<Player> players;
private ObjectInputStream in;

public ServerReceiver(List<Game> games, List<Player> players, ObjectInputStream in) throws IOException{
    this.games = games;
    this.players = players;
    this.in = in;
}

@Override
public void run(){
    CommandMessage message = null;
    System.out.println("in thread");
    while(true){
        try {
            message = (CommandMessage) in.readObject();

        } catch (IOException ex) {
            Logger.getLogger(ServerReceiver.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServerReceiver.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

}

客户端:

public static void main(String[] argv) throws IOException, ClassNotFoundException{
    socket = new Socket("localhost",9999);
    System.out.println("Connected");

    in = new ObjectInputStream(socket.getInputStream());
    Integer id = (Integer) in.readObject();
    System.out.println(id);
    out = new ObjectOutputStream(socket.getOutputStream());
    out.flush();

    CommandMessage m = new CommandMessage(0, 0, null);
    m.setName("Name");
    out.writeObject(m);

}

和服务器输出:

Waiting for player
Player connected, waiting for command
Output created
input created
Waiting for player
in thread
III 16, 2014 12:24:20 DOP. Comunication.ServerReceiver run
SEVERE: null
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException:   Comunication.CommandMessage
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
at Comunication.ServerReceiver.run(ServerReceiver.java:40)
Caused by: java.io.NotSerializableException: Comunication.CommandMessage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at Client.main(Client.java:37)

客户端输出:

Connected
0
Exception in thread "main" java.io.NotSerializableException: Comunication.CommandMessage
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at Client.main(Client.java:37)
Java Result: 1

我该怎么办?或者如果我的想法不好,有人可以告诉我如何做得更好吗?

3 个答案:

答案 0 :(得分:0)

错误信息非常清楚。您的CommandMessage类不实现Serializable。解决了这个问题。

答案 1 :(得分:0)

你在循环中读取一个对象,但是当客户端断开连接时,你必须以某种方式处理异常并且不要从ObjectInputStream中读取其他对象

答案 2 :(得分:0)

要序列化的对象,通过网络发送时所执行的操作必须实现Serializableable:http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html