Connection reset 连接被重置的解决方法

流是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 reset
	at 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\n
			String 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:您好

 

你可能感兴趣的:(java,字符流,连接被重置)