ObjectInputStream:StreamCorruptedException [英] ObjectInputStream : StreamCorruptedException

查看:74
本文介绍了ObjectInputStream:StreamCorruptedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hi everbody i have to write a server which communicate over a socket connection. The client sends Objects to the server and the server prints it to console.

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:

But i always become a StreamCorruptedException at this line:

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

有人可以帮助我吗?

谢谢

推荐答案

我正在通过telnet连接到服务器并发送普通文本输入.

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

因此另一端根本没有使用 ObjectOutputStream ,因此使用 ObjectInputStream 只是胡说八道.

So the other end isn't using ObjectOutputStream at all, so using ObjectInputStream is just nonsense.

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

If you want to just read text, use BufferedReader.readLine().

这篇关于ObjectInputStream:StreamCorruptedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆