日期:2014-05-20  浏览次数:20702 次

关于 socket 编程的问题 在线等答案 大神们
客户端代码
server = new Socket(InetAddress.getLocalHost(), 3134);  
        System.out.println(InetAddress.getLocalHost());
        BufferedReader in = new BufferedReader(new InputStreamReader(  
                server.getInputStream()));  
        PrintWriter out = new PrintWriter(server.getOutputStream());    
        while (true) {  
          System.out.print("客户端:");
          BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));
            String str = wt.readLine();  
            out.println("客户端:"+str);  
            out.flush();  
            if (str.equals("end")) { 
             out.println("客户端申请退出!");     //前面print加了ln 
                                                     //这里打印不出来!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                break;  
            }  
            System.out.println(in.readLine());  
        }  
        server.close();  
    }  



服务器代码
ServerSocket server = new ServerSocket(3134);  
        Socket client = server.accept();  
        BufferedReader in = new BufferedReader(new InputStreamReader(  
                client.getInputStream()));  
        PrintWriter out = new PrintWriter(client.getOutputStream());  
        while (true) { 
            String str = in.readLine();  
            System.out.println(str);  
            out.print("has receive....");   //加了ln后  后面的输出打印不出来
                                            //即:out.println("服务器:"+s);内容打印不出!!!!!!!!!!!!!!!!!!!!!!!!!!!
            System.out.print("服务器:");
            BufferedReader br = new BufferedReader(new InputStreamReader(  
                   System.in));
            String s=br.readLine();
            out.println("服务器:"+s); 
            out.flush(); 
            if (str.equals("end")||s.equals("end"))  
                break;  
        }  
        client.close();
}



问题是:第一个println里面的内容打印出来了 后面的就打印不出来了? 若是去掉ln改为print就打印的出来  为嘛啊?我菜鸟 求大神给个解释!!!!
------解决方案--------------------

BufferedReader wt = new BufferedReader(new InputStreamReader(System.in));

BufferedReader br = new BufferedReader(new InputStreamReader(  
                   System.in));
都放到while循环之前
------解决方案--------------------
把客户端的out.flush语句放到close前面 试试行不行