gpt4 book ai didi

java - 对象输入流 : StreamCorruptedException

转载 作者:行者123 更新时间:2023-12-01 15:28:53 25 4
gpt4 key购买 nike

大家好,我必须编写一个通过套接字连接进行通信的服务器。客户端将对象发送到服务器,服务器将其打印到控制台。

public class ConnectionListener {

ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
Object message;

void runListener()
{
try{
providerSocket = new ServerSocket(2004, 10);
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
in = new ObjectInputStream(connection.getInputStream());
out = new ObjectOutputStream(connection.getOutputStream());
do{
message = in.readObject();
System.out.println("client>" + message);
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}

}

但我总是在这一行变成 StreamCorruptedException:

in = new ObjectInputStream(connection.getInputStream());

有人可以帮我吗?

谢谢

最佳答案

I´m connecting to the server via telnet and sending normal text input.

所以另一端根本没有使用ObjectOutputStream,所以使用ObjectInputStream只是废话。

如果您只想读取文本,请使用BufferedReader.readLine()

关于java - 对象输入流 : StreamCorruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9820859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com