socket通信异常 java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了socket通信异常 java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)相关的知识,希望对你有一定的参考价值。

客户端通信时,先用OutputStream发送了一段文字
然后Server端接受
客户端再不断用ObjectOutputStream发送序列化的coordinate对象。

Server:
try
ServerSocket ss = new ServerSocket(56);
System.out.println("正在监听"+ss.getLocalPort()+"号端口。");
while (true)
try
Socket socket = ss.accept();
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line = br.readLine();
System.out.println(line);
Thread.sleep(2000);
new Thread(new driverThread(socket)).start();

catch(Exception ex)
ex.printStackTrace();



catch(Exception e)
e.printStackTrace();


driverThread是不断接收客户端发来的coordinate对象。
本来没有前面这段客户端用OutputStream发送文字,服务器接受时是好好的。
但是加了这一段出现这个异常。
正在监听56号端口。
您好,您收到了服务器的新年祝福!
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at servers.driverThread.<init>(driverThread.java:23)
at servers.start.main(start.java:21)
附:
driverThread.java:23:
is = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
start.java:21:
new Thread(new driverThread(socket)).start();
查了下好像不能用多个IO流,我这里先用OutputStream,再用ObjectOutputStream是不是不对?

参考技术A 是的,一个输入、一个输出追问

输入输出流没有问题吧,有输出肯定就有输入啊。但是用了两种输出类型,一种输出bytes,输出完下面输出对象。

追答

socket.getInputStream()


socket.getOutputStream()

各一个。

本回答被提问者采纳

Socket简单通信

服务端代码:

public class MyServer {
    private ServerSocket serverSocket;

    public MyServer() {
        try {
            // 设置你服务器监听的端口为10000,用户能使用的端口为1025-65535
            serverSocket = new ServerSocket(10001);
        } catch (IOException e) {
            // 捕获异常,不懂的话好好看java,
            e.printStackTrace();
        }
        // 创建新的监听主线程,这个线程创建ServerSocket监听
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    Socket socket = null;
                    try {
                        socket = serverSocket.accept();
                        // 当监听到了客户端连接后,创建新线程传输数据,这样可以实现多个客户端同时访问
                        new Thread(new SocketHandler(socket)).start();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    class SocketHandler implements Runnable {
        private Socket socket;
        private BufferedReader reader;
        private PrintWriter writer;

        SocketHandler(Socket socket) {
            try {
                this.socket = socket;
                reader = new BufferedReader(new InputStreamReader(
                        this.socket.getInputStream(), "GB2312"));
                writer = new PrintWriter(socket.getOutputStream(), true);
                writer.println("-------welcome---------");
                writer.println("-------welcome---------");
                writer.println("-------welcome---------");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        // 这里是覆盖实现接口Runnable里的run()
        public void run() {
            try {
                // 读取数据,这里只能读取一行String
                String line = reader.readLine();
                System.out.println(line);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 最后要关闭Socket
                try {
                    if (socket != null)
                        socket.close();
                    if (reader != null)
                        reader.close();
                    if (writer != null)
                        writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        new MyServer();
    }

}

客户端代码:

public class SocketClientDemo implements Runnable {
    private Socket socket;
    BufferedReader reader;
    private PrintWriter writer;

    public SocketClientDemo() {
        try {
            // 127.0.0.1表示本机IP,10000为服务器Socket设置的端口
            socket = new Socket("127.0.0.1", 10001);
            reader = new BufferedReader(new InputStreamReader(
                    socket.getInputStream(), "GB2312"));
            writer = new PrintWriter(socket.getOutputStream(), true);
            writer.println("working.............");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        try {
            // 这里就可以读取所有行String
            String line, buffer = "";
            while (!((line = reader.readLine()) == null))
                buffer += line;
            System.out.println(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("problem");
        } finally {
            // 最后关闭Socket
            try {
                if (socket != null)
                    socket.close();
                if (reader != null)
                    reader.close();
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        new Thread(new SocketClientDemo()).start();

    }

}

 

以上是关于socket通信异常 java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)的主要内容,如果未能解决你的问题,请参考以下文章

Socket简单通信

if(socket!=null)socket.close();题目要求是通信结束,关闭socket

Android 基于UDP的Socket通信

java中Socket通信

网络编程——通信循环链接循环粘包问题

如何检查oracle数据库端的socket通信中断