java实现——客户端登录



package mytest6_29;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
//客户端
public class Test4 {

	public static void main(String[] args) throws Exception 
	{
		System.out.println("客户端启动");
		// TODO Auto-generated method stub

		String ip = InetAddress.getLocalHost().getHostAddress();//获取本机的IP
		System.out.println(ip);
		
		Socket s = new Socket("172.24.59.1",10013);//新建传送端
		//建立键盘录入
		BufferedReader bur = 
				new BufferedReader(new InputStreamReader(System.in));
		//将读取到的数据发送给指定服务端
		PrintWriter out = new PrintWriter(s.getOutputStream(),true);
		//读取服务端返回的信息
	e09BufferedReader bufin = 
				new BufferedReader(new InputStreamReader(s.getInputStream()));
		for(int x = 0;x<3;x++)
		{
			String line =bur.readLine();
			if(line ==null)
				break;
			out.println(line);
			String lines = bufin.readLine();
			System.out.println(lines);
		}
	}

}

//新建服务端
//使用多线程来实现
public class Test5 

	{
		
		public static void main(String[] args) throws Exception
		{
			System.out.println("服务端启动");
			ServerSocket ss  = new ServerSocket(10013);
			while(true)
			{
				Socket  s = ss.accept();
				new Thread(new User(s)).start();
			}
		}
		}
class User implements Runnable
{
	private Socket s;

	User(Socket s)
	{
		this.s = s;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		for(int x = 0;x<3;x++)
		{
			//获取连接服务端的客户端的IP
			String ip = s.getInetAddress().getHostAddress();
			System.out.println(ip+"进入");
			
			try {
				//读取客户端录入的信息
				  BufferedReader buf = new BufferedReader(new InputStreamReader(s.getInputStream()));
				  String name = buf.readLine();
				//关联本地数据库
				  BufferedReader bufr = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\测试文件夹\\11.txt"));
				//向客户端输出反馈信息
				  PrintWriter out = new PrintWriter(s.getOutputStream(),true);
				  
				  String lin = null;
				  boolean flag = false;
				  while((lin = bufr.readLine())!=null)
				  {
					  if(lin.equals(name))
					  {
						  flag = true;
						  System.out.println("用户存在");
						  break;
					  }
				  }
				  if(flag)
				  {
					  System.out.println(name+"忆登录");
					  out.println(name+"欢迎便当");
				  }
				  else
				  {
					  System.out.println(name+"未登录");
					  out.println(name+"用户名不在");
				  }
			} catch (IOException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
}








早起的年轻人 CSDN认证博客专家 移动开发 项目管理 Java
只要用心去做,每一件事情还是有可能成功的,当然成功是没有界限的,只不过是达到自己心里的那个目标,公众号:我的大前端生涯,一个爱喝茶的程序员,通常会搞搞SpringBoot 、Herbinate、Mybatiys、Android、iOS、Flutter、Vue、小程序等.
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页