Connection reset异常
package Dsocket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) {
//创建一个socket对象,绑定其端口号和Ip地址
try {
Socket socket = new Socket("localhost",30000);
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);//将字节输出流转换成字符输出流。
pw.write("用户名:sgeg;密码:6789298");
pw.flush();
socket.shutdownInput();//关闭输出流
//获取字节输入流,读取服务器端的信息。
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader
(new InputStreamReader(is));
String s = null;
while((s=br.readLine())!=null){
System.out.println("from server; client:"+s);
}
socket.shutdownInput();
//关闭相关资源
br.close();
is.close();
pw.close();
os.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在Client中报错。报错代码如下:
报错代码提示:while((s=br.readLine())!=null){
服务器已开启,等待客户端连接
客户端的数量:1
计算机的ip地址为:127.0.0.1
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at Dsocket.ServerThread.run(ServerThread.java:30)