问答详情
源自:3-4 完善用户登陆之服务器响应客户端

运行Client端报错

Server

package socket;


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.ServerSocket;

import java.net.Socket;


/*

 * 基于TCP协议的服务端

 */

public class TCPServer {


public static void main(String[] args) {

try {

//1、创建一个服务器端的Socket,即ServerSocket,指定绑定的端口,并对其监听

ServerSocket socket = new ServerSocket(5000);

//2、调用accept()方法对其监听,并等待客户端的响应

System.out.println("****服务器即将启动,等待客户端的连接****");

Socket socket2 = socket.accept();//在没有连接前,处于阻塞状态

//3、获取输入流,并等待客户端的响应

InputStream iStream = socket2.getInputStream();

InputStreamReader iReader = new InputStreamReader(iStream);//把字节流转换成字符流

BufferedReader bReader = new BufferedReader(iReader);//缓存存储

String info = null;

while((info = bReader.readLine())!=null){

System.out.println("我是服务器,客户端发送的信息:"+info);

}

socket2.shutdownInput();

//4、获取输出流,响应客户端的响应

OutputStream oStream = socket2.getOutputStream();

PrintWriter pWriter = new PrintWriter(oStream);

pWriter.write("欢迎你登入!!!");

pWriter.flush();

//5、关闭资源

oStream.close();

pWriter.close();

bReader.close();

iReader.close();

iStream.close();

socket.close();

socket2.close();

} catch (IOException e) {

e.printStackTrace();

}


}


}

Client

package socket;


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;


/*

 * 基于TCP协议的客户端

 */

public class TCPClient {


public static void main(String[] args) {

try {

//1、创建客户端的Socket,并指定地址和端口

Socket socket = new Socket("localhost", 5000);

//2、获取输出流,向服务器发送信息

OutputStream oStream = socket.getOutputStream();//字节输出流

PrintWriter pWriter = new PrintWriter(oStream);//将输出流转换成打印流

pWriter.write("用户名:admin;密码:123");

pWriter.flush();

socket.shutdownInput();

//3、接收服务器发送来的反馈信息

InputStream iStream = socket.getInputStream();

//InputStreamReader inputStreamReader = new InputStreamReader(iStream);//将字节流转换成字符流

BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));

String info = null;

while((info = bReader.readLine())!=null){

System.out.println("我是客户端,服务器反馈的信息:"+info);

}

//4、关闭资源

iStream.close();

bReader.close();

pWriter.close();

oStream.close();

socket.close();

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}


}


}

按照视频讲的代码来写的,Server可以正常运行,但运行Client就会报这个错误

java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:196)

at java.net.SocketInputStream.read(SocketInputStream.java:122)

at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)

at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)

at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)

at java.io.InputStreamReader.read(InputStreamReader.java:184)

at java.io.BufferedReader.fill(BufferedReader.java:154)

at java.io.BufferedReader.readLine(BufferedReader.java:317)

at java.io.BufferedReader.readLine(BufferedReader.java:382)

at socket.TCPServer.main(TCPServer.java:30)

有没有知道怎么回事

提问者:天翼飞虹 2016-10-24 20:33

个回答

  • DomiZhuan
    2016-10-24 21:44:36
    已采纳

    /*

     * 基于TCP协议的客户端

     */

    public class TCPClient {


    public static void main(String[] args) {

    try {

    // 1、创建客户端的Socket,并指定地址和端口

    Socket socket = new Socket("localhost", 5000);

    // 2、获取输出流,向服务器发送信息

    OutputStream oStream = socket.getOutputStream();// 字节输出流

    PrintWriter pWriter = new PrintWriter(oStream);// 将输出流转换成打印流

    pWriter.write("用户名:admin;密码:123");

    pWriter.flush();

    socket.shutdownOutput();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    // 3、接收服务器发送来的反馈信息

    InputStream iStream = socket.getInputStream();

    // InputStreamReader inputStreamReader = new

    // InputStreamReader(iStream);//将字节流转换成字符流

    BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));

    String info = null;

    while ((info = bReader.readLine()) != null) {

    System.out.println("我是客户端,服务器反馈的信息:" + info);

    }

    // 4、关闭资源

    iStream.close();

    bReader.close();

    pWriter.close();

    oStream.close();

    socket.close();

    } catch (UnknownHostException e) {

    e.printStackTrace();

    } catch (IOException e) {

    e.printStackTrace();

    }


    }


  • 遇见花开遇见你啊
    2017-02-06 14:50:36

    到底是改了哪里?我的也报这样的错误