java TCP 从客户端键入信息 循环接收发送 示例

当服务器端的监听在 循环里面的时候 发现 不正正常接收客户端的信息
把监听accept放在循环外面却可以,刚学到这里,不是很理解!
大神路过帮忙解答下,谢谢!

public class TcpClient {
    public static void main(String[] args) {
        //创建套接字
        Socket s = null;
        //创建键入字符流
        BufferedReader reader = null;
        //创建输出字符流
        BufferedWriter writer = null;

        try {
            //实例化套接字
            s = new Socket("127.0.0.1",10011);
            System.out.println("TCP 已启动 请输出您要发送的信息......");
            //获得此套接字输出流
            OutputStream out = s.getOutputStream();
            //实例化输入流
            reader = new BufferedReader(new InputStreamReader(System.in));
            //实例化输出流
            writer = new BufferedWriter(new OutputStreamWriter(out));
            //保存读入信息
            String line = null;
            while (true){

                line = reader.readLine();
                writer.write(line);
                writer.newLine();
                writer.flush();

                if (line.equals("exit")){
                    break;
                }

                System.out.println("循环被执行了");
            }
            System.out.println("TCP 客户端结束运行!");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (s != null){
                    s.close();
                }
                if (writer != null){
                    writer.close();
                }
                if (reader != null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class TcpServer {
    public static void main(String[] args) {
        //创建服务端套接字
        ServerSocket ss = null;
        //创建缓冲字符流
        BufferedReader reader = null;
        try {
            //实例化套接字并绑定端口
            ss = new ServerSocket(10011);
            System.out.println("TCP 服务已经启动......");
            //监听客户端对象
            Socket s = ss.accept();
            System.out.println("连接成功......");

            //获得客服端套接字的输入流对象
            InputStream in = s.getInputStream();
            //装换成缓冲字符流
            reader = new BufferedReader(new InputStreamReader(in));

            //获取客户端ip地址
            InetAddress inetAddress = s.getInetAddress();
            String hostAddress = inetAddress.getHostAddress();

            while (true) {
                //读取输入流
                String line = reader.readLine();

                //设置退出信号
                if (line.equals("exit")){
                    break;
                }
                System.out.println(hostAddress + " 发送了:" + line);
            }
            System.out.println("TCP 服务结束运行!");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
               if (ss !=null){
                    ss.close();
                }
                if (reader != null){
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页