import java.io.*;
import java.net.*;class Client {
    public static void main(String args[]) throws Exception{
        Socket server = new Socket("127.0.0.1",4567);
        System.out.println("Connected with Server..");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(server.getOutputStream());
        out.println("ok?????");
        out.flush();
        int choice = new Client().display();
        String ss;
        System.out.println(choice);
        while(choice!=2){
         if(choice == 0){
         System.out.println("输入用户名密码,用,分隔");
         out.println(br.readLine());
out.flush();
     System.out.println("what?fuck");
         }
         else if(choice == 1){
         System.out.println("输入新用户名密码,用,分隔");
ss = br.readLine();
         out.println(ss);
         out.flush();
         }
         choice = new Client().display();
        }
       
        server.close();  
    }
   public int display() throws Exception{
        System.out.println("0.User Login");
        System.out.println("1.Register new User");
        System.out.println("3.quit");
//System.out.println((int)(System.in.read()));
        return (int)(System.in.read())-48;
   }
}
choice == 0 的时候 直接跳过out.println(br.readLine());
纠结 求解释

解决方案 »

  1.   

    import java.io.*;
    import java.net.*;public class Server {
      public static void main(String args[]) throws Exception{
      ServerSocket server = new ServerSocket(4567);
      System.out.println("Server is ready...please Open the Client");
      Socket client = server.accept();//侦听并接收到 Server的连接,accept产生一个阻塞,直到等待到一个连接,之后返回一个客户端的Socket实例   
      System.out.println("client is Open ..Server is working");
      BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
      String msg = br.readLine();
      System.out.println("msg" + msg);
      //if(msg.startsWith("0")){}
      while(!msg.equals("quit")){
           System.out.println("you got message from client: " + msg);   
           msg = br.readLine();
      }
      br.readLine();
      System.out.println("client " + msg + "ed");   
      bw.flush();
      server.close();
      client.close();
     }
    }server接收到的是空的字符串
      

  2.   

    运行结果发一个,一运行就直接“what?fuck”?呵呵
      

  3.   

    if(choice == 0)
    后加断点,单步调试看看
      

  4.   

    木有跳过,只是这个br.readLine()是空字符串的
      

  5.   

    程序应该没有错吧,你现在好像是Server端拿到Client客户端的输入输出流,所有你运行Client端的时候是空值,需要读取输入的。
      

  6.   

    不要沉帖 不要沉帖,,,,
    现在的问题就是client端 br.readLine() 不等待键盘输入,server端接收到的直接是空的字符串,
    怎么回事呢?
      

  7.   

    用Scannerpublic int display() throws Exception {
    System.out.println("0.User Login");
    System.out.println("1.Register new User");
    System.out.println("3.quit");
    Scanner console=new Scanner(System.in);
    int choice=console.nextInt();
    return choice;
    }
      

  8.   

    隐身你输入0后,接着按了enter键,所以这个时候键盘是输入了的,这样br.readLine()就不用等键盘输入了,并且读到的换行也就是空字符串 System.out.println("输入用户名密码,用,分隔");
    int str2 = br.read();
    int str3 = br.read();
    out.println(br.readLine());
    out.flush();你debug看下,发现str2是13,str3为10,所以得到是空字符串就知道什么原因了。