黑马程序员技术交流社区

标题: TCP问题 我传到服务端的数据总是为null 实在找不到问题 哥... [打印本页]

作者: walkonby    时间: 2013-2-18 18:53
标题: TCP问题 我传到服务端的数据总是为null 实在找不到问题 哥...
本帖最后由 张政 于 2013-2-18 21:58 编辑
  1. import java.io.*;
  2. import java.net.*;
  3. class LoginClient{
  4.         public static void main(String[] args)throws IOException{
  5.                 Socket sk = new Socket("192.168.1.101",10008);
  6.         
  7.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.                 BufferedReader brin = new BufferedReader(new InputStreamReader(sk.getInputStream()));
  9.                 PrintWriter out = new PrintWriter(sk.getOutputStream(),true);
  10.                
  11.                 String line = null;
  12.                 for(int i = 0; i<3; i++){
  13.                         line = br.readLine();
  14.                         System.out.println(line);
  15.                         out.write(line);
  16.                         
  17.                         //关闭Socket服务输出流
  18.                         sk.shutdownOutput();
  19.                         
  20.                         line = brin.readLine();
  21.                         System.out.println(line);
  22.                         if(line.contains("欢迎"))
  23.                                 break;
  24.                 }
  25.         }
  26. }
  27. class LoginServer{
  28.         public static void main(String[] args) throws IOException {
  29.                 //创建服务端socket服务,并绑定端口
  30.                 ServerSocket ss = new ServerSocket(10008);
  31.                 Socket sk = ss.accept();
  32.                 new Thread(new Login(sk)).start();
  33.                
  34.         }
  35. }


  36. class Login implements Runnable{
  37.         private Socket sk;
  38.         Login(Socket sk){
  39.                 this.sk = sk;
  40.         }
  41.         public void run(){
  42.                 String ip = sk.getInetAddress().getHostAddress();
  43.                 System.out.println(ip + ".......connected");
  44.         
  45.                 try{
  46.                         BufferedReader brin = new BufferedReader(new InputStreamReader(sk.getInputStream()));
  47.                         PrintWriter out = new PrintWriter(sk.getOutputStream());               
  48.                         BufferedReader br = new BufferedReader(new FileReader("user.txt"));
  49.                                                 

  50.                         String name = null;
  51.                         boolean flag = false;
  52.                         for(int i = 0; i<3; i++){
  53.                                 name = brin.readLine();
  54.                                         if(name == null){
  55.                                                 System.out.println("名字为空");
  56.                                                 break;
  57.                                         }
  58.                                 while(true){
  59.                                         if(name.equals(br.readLine())){
  60.                                                 flag = true;
  61.                                                 break;
  62.                                         }
  63.                                 }
  64.                                 if(flag){
  65.                                         System.out.println(name + "登陆");
  66.                                         out.println(name + "欢迎登陆");
  67.                                         break;
  68.                                 }else{
  69.                                         System.out.println(name + "尝试登陆");
  70.                                 }
  71.                         }
  72.                         
  73.                 }catch(IOException e){
  74.                         throw new RuntimeException("校正失败");
  75.                 }
  76.         }
  77.         
  78. }
复制代码

作者: 温立华    时间: 2013-2-18 19:08
{:soso_e103:}
作者: 杨玲    时间: 2013-2-18 19:11
呵呵,你在循环中关闭流,你觉得应该吗?
作者: 柴乔军    时间: 2013-2-18 19:23
PrintWriter的write方法不带自动刷新
作者: walkonby    时间: 2013-2-18 21:58
问题解决了  杨铃兄和乔军兄说的问题都存在
其他的地方还有4 5处的小BUG  至于是什么地方我就不说了  
感兴趣的哥们就拿我的这个满是BUG的程序练练手把  只需要自己建一个user.txt就行了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2