Connection reset 连接被重置的解决方法
编程技术  /  houtizong 发布于 3年前   100
流是java的核心部分,,昨天在做android服务器连接服务器的时候出了问题,就将代码放到java中执行,结果还是一样连接被重置
被重置的代码如下;
客户端代码;
package 通信软件服务器;import java.io.BufferedWriter;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.Socket;//客户端public class SocketDemo {//private static String text="您好";public static void main(String[] args) { //Connection reset 连接被重置try{//创建套接字的连接对象Socket socket = new Socket("127.0.0.1",9090);OutputStream os = socket.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);BufferedWriter bdw = new BufferedWriter(osw);//String text="您好";bdw.write(text, 0, text.length());bdw.flush();}catch(Exception e){e.printStackTrace();}}}
服务器代码;
package 通信软件服务器;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class Demo {public static void main(String[] args) {try {ServerSocket server = new ServerSocket(9090);System.out.println("等待客户端连接");Socket socket = server.accept();System.out.println(socket.getRemoteSocketAddress() + "连接上服务器"); InputStream is = socket.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader bis = new BufferedReader(isr);String line = bis.readLine();System.out.println("line:" + line);} catch (Exception e) {e.printStackTrace();}}}
客户端连接服务器会出现以下结果;
等待客户端连接/127.0.0.1:53373连接上服务器java.net.SocketException: Connection resetat java.net.SocketInputStream.read(Unknown Source)at java.net.SocketInputStream.read(Unknown Source)at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)at sun.nio.cs.StreamDecoder.implRead(Unknown Source)at sun.nio.cs.StreamDecoder.read(Unknown Source)at java.io.InputStreamReader.read(Unknown Source)at java.io.BufferedReader.fill(Unknown Source)at java.io.BufferedReader.readLine(Unknown Source)at java.io.BufferedReader.readLine(Unknown Source)at 通信软件服务器.Demo.main(Demo.java:24)
原因分析:
客户端在连接到服务器之后就已经被虚拟机回收了
解决思路;
1,在客户端中关闭流
客户端在连接到服务器之后,流必须发送字符串之后才能被关闭和回收
bdw.close();
2,在发送的字符串穿件分割符;
在发送字符串的时候添加分隔符,告诉服务器一行的结束位置; \r\n是换行符
String text="您好\r\n";
修改后的代码;
服务器
package 通信软件服务器;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.ServerSocket;import java.net.Socket;public class Demo {public static void main(String[] args) {try {ServerSocket server = new ServerSocket(9090);System.out.println("等待客户端连接");Socket socket = server.accept();System.out.println(socket.getRemoteSocketAddress() + "连接上服务器"); InputStream is = socket.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader bis = new BufferedReader(isr);//int a=bis.read();//System.out.println(a);//readLine()必须要有结束的标志\r\nString line = bis.readLine();System.out.println("line:" + line);} catch (Exception e) {e.printStackTrace();}}}
客户端;
package 通信软件服务器;import java.io.BufferedWriter;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.net.Socket;public class SocketDemo {//private static String text="您好";public static void main(String[] args) { //Connection reset 连接被重置try{//创建套接字的连接对象Socket socket = new Socket("127.0.0.1",9090);OutputStream os = socket.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);BufferedWriter bdw = new BufferedWriter(osw);//String text="您好\n\r";bdw.write(text, 0, text.length());bdw.flush(); bdw.close();}catch(Exception e){e.printStackTrace();}}}
执行结果:
等待客户端连接/127.0.0.1:53633连接上服务器line:您好
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接