A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import java.net.*;
import java.io.*;
import java.lang.Thread;
class LoginClient
{
public static void main(String[]args)throws Exception
{
      Socket  st=new Socket("192.168.0.2",10002);
      BufferedReader bur=null;
      try{
          PrintWriter out=new PrintWriter(st.getOutputStream(),true);
          bur=new BufferedReader(new InputStreamReader(System.in));
          BufferedReader buw=new BufferedReader(new InputStreamReader(st.getInputStream()));
            for(int i=0;i<3;i++)
              {
                 String name=bur.readLine();
     if(name==null)// 如果用户直接输入回车的话  name应该为空吧然后continue  继续等待用户输入但是实验证明它不为空而且传到了服务端  但是服务端打印name又什么都  没有
     {
     System.out.println("请输入用户名");
     continue;
     }
     out.println(name);
     String line=buw.readLine();
     System.out.println(line);
     if(line.contains("欢迎"))
     break;
     }
  }
catch(IOException e){e.printStackTrace();}
finally{
try{
bur.close();
st.close();
}
catch(IOException e){e.printStackTrace();}
}
}
}
class ThreadLogin implements Runnable
{
    private Socket st=null;
ThreadLogin(Socket st)
{
this.st=st;
}
public void run()
{
   String ip=st.getInetAddress().getHostAddress();
   BufferedReader bur=null;
   System.out.println(ip+"--------------请求连接服务器");
   try{
       //bur=new BufferedReader(new FileReader("user.txt"));
    PrintWriter out=new PrintWriter(st.getOutputStream(),true);
    BufferedReader burr=new BufferedReader(new InputStreamReader(st.getInputStream()));
    boolean flag=false;
    String userName=null;
   // String name=null;
       for(int i=0;i<3;i++)
    {
   
      String name=burr.readLine();
    if(name==null)
    break;
    bur=new BufferedReader(new FileReader("user.txt"));
    while((userName=bur.readLine())!=null)
    {
       if(userName.equals(name))
       {
          flag=true;
       break;
       }
    }
    if(!flag)
    {
      System.out.println("注意注意"+name+"正在尝试登陆!");
      out.println("用户名错误,请重新登录");
      bur.close();
    }
    else
    {
       out.println("欢迎"+userName+"登录");
       System.out.println(userName+"已经成功登录");
       break;
    }
   }
  }
  catch(IOException e){ e.printStackTrace();}
  finally{
     try
     {
       st.close();
    bur.close();
     }
     catch(IOException e){e.printStackTrace();}
     }
}
}
class LoginServer
{
public static void main(String[]args)
{
    try{
    ServerSocket st=new ServerSocket(10002);
    while(true)
      {
       Socket ss=st.accept();
    new Thread(new ThreadLogin(ss)).start();
  }
  }
  catch(IOException e){e.printStackTrace();}
  }
}
这个程序有个bug如下代码
if(name==null)// 如果用户直接输入回车的话  name应该为空吧然后continue  继续等待用户输入但是实验证明它不为空而且传到了服务端  但是服务端打印name又什么都  没有
     {
     System.out.println("请输入用户名");
     continue;
     }

3 个回复

倒序浏览
直接回车的话应该还是有输入内容的吧     \r\n    也就是回车符
回复 使用道具 举报
回车也代表字符.. .. "\r\n"
回复 使用道具 举报
readLine()
    读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。
返回:
    包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马