模拟用户登录的功能TCP协议

模拟用户登录的功能 实现客户发送登录用户信息,服务器端显示登录信并响应给客户端登录成功

服务器端

public class Server {
public static void main(String[] args) throws Exception {
    ServerSocket ss = new ServerSocket(8090);
    Socket s = ss.accept();
    InputStream is=s.getInputStream();
    OutputStream os=s.getOutputStream();
    BufferedReader  br= new BufferedReader(new InputStreamReader(is));
     String info=null;
     while(!((info=br.readLine())==null)){
         System.out.println("我是服务器,客户登录信息是"+info);
     }
     String reply="欢迎你,登录成功!";
     os.write(reply.getBytes());
     br.close();
     os.close();
     is.close();
     s.close();
     ss.close();
  }
}

客户端

public class Client {
public static void main(String[] args) throws Exception {
    Socket socket=new Socket("localhost",8090);
    
    OutputStream os=socket.getOutputStream();
    InputStream is=socket.getInputStream();
    
    String info="用户名:sy;用户密码:123456";
    os.write(info.getBytes());
    socket.shutdownOutput();
    
    String reply=null;
    BufferedReader br=new BufferedReader(new InputStreamReader(is));
    while(!((reply=br.readLine())==null)){
    System.out.println("我是客户端,服务器的响应为:"+reply);
    }
    br.close();
    is.close();
    os.close();
    socket.close();
}
}
 

猜你喜欢

转载自blog.csdn.net/Shi_Yuan_Csdn/article/details/81912598