【发布时间】:2012-03-22 10:54:27
【问题描述】:
大家好,我必须编写一个通过套接字连接进行通信的服务器。 客户端将对象发送到服务器,服务器将其打印到控制台。
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());
谁能帮帮我?
谢谢
【问题讨论】:
-
连接的另一端可能正在向您发送错误数据。从您给我们的信息中无法确定。
-
客户端通过连接发送什么?
ObjectInputStream需要一个序列化的 Java 对象。这是客户端发送的内容吗? -
你也可以发布发送方代码吗?
-
我正在通过 telnet 连接到服务器并发送正常的文本输入。
标签: java