socket is closed异常的原因及其解决方法

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/liyongqi_/article/details/52198795

在一个有关socket的程序中,客户端出现了 socket is closed异常,我百思不得其解,并没有关闭socket啊。

下面首先来分析一下这个异常出现的原因:该异常在客户端和服务器均可能发生。异常的原因是己方主动关闭了连接后(调用了Socket的close方法)再对网络连接进行读写操作。

而造成socket关闭常见的有以下两种:
1、调用了.close()方法关闭socket
2、关闭了输入输出流

下面来看一下我的错误代码

 BufferedWriter bufferedWriter=null;
                try {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

                } catch (IOException e) {
                    Log.d("Client","BufferedWriter出错");
                    e.printStackTrace();
                }
                // 获得EditTex的内容
                String text = mEditText.getText().toString();
                Log.e("Client","即将发送的信息为:"+text);
                Log.e("Client","haha");
                try {
                    // 发送数据
                    if (bufferedWriter != null) {
                        bufferedWriter.write(text+"\r\n");
                    }
                    bufferedWriter.flush();
                    bufferedWriter.close();
                } catch (IOException e) {
                    Log.e("Client","发送数据出错");
                    e.printStackTrace();
                }
                Log.e("Client", "成功发送数据");
                // 清空内容
                mEditText.setText("");
                Log.e("Client", "下面开始读取数据");
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));
                    Log.e("Client","完毕");
                    String s=in.readLine();
                    Log.e("Client",s);
                } catch (IOException e) {
                    Log.e("Client",e.getMessage());
                }

可以看到在用write()方法传送完成后,又调用了bufferedWriter.close();正是因为这一行代码,导致socket closed。而下面BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));因此出现异常

那么我们的解决方式是将bufferedWriter.close();下移到末尾处即可如下:

 BufferedWriter bufferedWriter=null;
                try {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

                } catch (IOException e) {
                    Log.d("Client","BufferedWriter出错");
                    e.printStackTrace();
                }
                // 获得EditTex的内容
                String text = mEditText.getText().toString();
                Log.e("Client","即将发送的信息为:"+text);
                Log.e("Client","haha");
                try {
                    // 发送数据
                    if (bufferedWriter != null) {
                        bufferedWriter.write(text+"\r\n");
                    }
                    bufferedWriter.flush();

                } catch (IOException e) {
                    Log.e("Client","发送数据出错");
                    e.printStackTrace();
                }
                Log.e("Client", "成功发送数据");
                // 清空内容
                mEditText.setText("");
                Log.e("Client", "下面开始读取数据");
                try {
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream(),"UTF-8"));
                    Log.e("Client","完毕");
                    String s=in.readLine();
                    Log.e("Client",s);
                } catch (IOException e) {
                    Log.e("Client",e.getMessage());
                }
展开阅读全文

没有更多推荐了,返回首页