套接字关闭异常 [英] Socket closed exception

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

问题描述

我写了一个简单的服务器和客户端示例,如下所示。

I wrote a simple server and client example as below .

客户端:


  • 打开连接

  • 获取outputstream,写入流并关闭输出流

  • 获取inputstream并从流中读取。此时获取异常

  • Open a connection
  • Get outputstream , write to stream and close the output stream
  • Get inputstream and read from the stream. Getting exception at this point

public class DateServer {
public static void main(String[] args) throws InterruptedException {
ServerSocket serverSocket = null;
Socket client = null;
try {
    serverSocket = new ServerSocket(6013);
    while (true) {
        client = serverSocket.accept();
        OutputStream outputStream = client.getOutputStream();
        InputStream inputStream = client.getInputStream();

        System.out.println("" + outputStream + "-" + inputStream);

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));

        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out
                    .println("Message recieved from client ::" + line);
        }

        PrintWriter printWriter = new PrintWriter(outputStream, true);

        printWriter.println(new java.util.Date().toString());

        client.close();
    }
} catch (IOException exception) {
    exception.printStackTrace();
    System.err.println(exception);
}
    }

    }


客户:

    public class DateClient {

public static void main(String[] args) throws UnknownHostException,
        IOException, InterruptedException {
    Socket sock = new Socket("127.0.0.1", 6013);
    OutputStream outputStream = sock.getOutputStream();
    InputStream inputStream = sock.getInputStream();

    System.out.println("" + outputStream + "-" + inputStream);

    PrintWriter printWriter = new PrintWriter(outputStream, true);
    printWriter.println("Hi Server");

    outputStream.close();

    System.out.println(sock.isConnected());

    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(inputStream));
    String line;
    while ((line = bufferedReader.readLine()) != null) { // Exception 
        System.out.println(line);
    }

}

}

在客户端获取下面的套接字关闭异常。

Getting below socket closed exception in Client . Could you please let me know what would be the reason.

  Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:146)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:282)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:324)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:176)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:153)
at java.io.BufferedReader.readLine(BufferedReader.java:316)
at java.io.BufferedReader.readLine(BufferedReader.java:379)
at edu.iub.cs.httpserver.DateClient.main(DateClient.java:32)


推荐答案

java.net.SocketException套接字已关闭
此异常表示您已关闭插座,然后继续尝试使用它。

java.net.SocketException socket is closed This exception means that you closed the socket, and then continued to try to use it.

os.close();

您在这里关闭了它。关闭套接字的输入或输出流会同时关闭另一个流和套接字。

And you closed it here. Closing either the input or the output stream of a Socket closes the other stream and the Socket.

这篇关于套接字关闭异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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