JAVA实现简陋聊天室为什么输出两次输入一次?

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**

  • 聊天室客户端
  • @author Administrator
    *
    */
    public class Client {

    private Socket socket;
    /**

    • 构造方法,用来初始化客户端
      */
      public Client() {
      try {

      System.out.println("正在连接服务端........");
      socket =new Socket("localhost",8088);
      System.out.println("已连接服务端");
      

      } catch (UnknownHostException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }
      }
      /**

    • 客户端开始工作的方法
      */
      public void start() {

      try {
      OutputStream out=socket.getOutputStream();
      OutputStreamWriter osw=new OutputStreamWriter(out,"UTF-8");
      BufferedWriter bw=new BufferedWriter(osw);
      PrintWriter pw=new PrintWriter(bw,true);
      Scanner scan=new Scanner(System.in);
      String line;
      System.out.println("输入exit即可退出服务器");

      while(true) {
          line=scan.nextLine();
          if("exit".equals(line.toLowerCase())) {
              pw.println("exit");
              break;
          }
          pw.flush();
          pw.println(line);
      }
      pw.close();
      

      }catch(Exception e) {
      e.printStackTrace();
      }
      }
      public static void main(String[] args) {
      Client client=new Client();
      client.start();
      }
      }

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**

  • 聊天室服务端
  • @author Administrator
    *
    */
    public class Server {

    private ServerSocket server;
    /**

    • 构造方法,用来初始化服务端 */ public Server() { try { System.out.println("正在启动客户端......"); server=new ServerSocket(8088); System.out.println("客户端启动完毕 !"); } catch (IOException e) { e.printStackTrace(); }

    }
    /**

    • 服务端开始工作的方法

      */
      public void start() {
      try {

      System.out.println("等待客户端连接......");
      Socket socket=server.accept();
      System.out.println("一个客户端连接了");
      InputStream in=socket.getInputStream();
      InputStreamReader isr=new InputStreamReader(in,"UTF-8");
      BufferedReader br=new BufferedReader(isr);  
      while(true) {
          if("exit".equals(br.readLine())) {
              System.out.println("客户端已退出");
      
              break;
          }
          System.out.println(br.readLine());
      }
      br.close();
      

      } catch (IOException e) {
      e.printStackTrace();
      }
      }

    public static void main(String[] args) {
    Server server=new Server();
    server.start();
    }

}

2个回答

readline是读取一行,你在判断和打印的时候都操作了readline,所以第二次才打印出来。server修改为

                String s = br.readLine();
                if ("exit".equals(s)) {
                    System.out.println("客户端已退出");
                    break;
                }
                System.out.println(s);
weixin_45213446
减白 已经解决.感谢!
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问