java TCP聊天室

启动页面:

public class TestStart {

	public static void main(String[] args) throws Exception {
		TestServerSocket ts = new TestServerSocket();
		new Thread(()-> {
			try {
				ts.reg();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
		new Thread(()->{
			try {
				ts.login();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}).start();
		
	}

}

服务器页面:

public class TestServerSocket {
	private  Properties userPro;
	public TestServerSocket(){
		File file = new File("Files\\userPro.properties");
		userPro = new Properties();
		try {
			userPro.load(new FileReader(file));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
		
		
	public void reg() throws Exception{
		ServerSocket server = new ServerSocket(2000);
		System.out.println("注册线程启动");
		while(true){
			Socket client = server.accept();
			new RegThread(client , userPro).start();
			
		}
	
	}
	public void login() throws Exception{
		ServerSocket server = new ServerSocket(4000);
		System.out.println("登录线程启动");
		while(true){
			Socket client = server.accept();
			new LoginThread(client , userPro).start();
			
		}
	}
	
	
	
}

注册线程:

public class RegThread extends Thread{
	
	Socket client;
	Properties userPro;

	public RegThread(Socket client, Properties userPro) {
		super();
		this.client = client;
		this.userPro = userPro;
	}



	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
			String message = br.readLine();
			String[] ss = message.split("#");
			String name = ss[0];
			String password = ss[1];
			
			if(userPro.containsKey(name)){
				pw.println("用户名已存在");
				pw.flush();
			}else{
				userPro.put(name, password);
				pw.println("注册成功");
				userPro.store(new FileWriter("Files\\userPro.properties"), null);
				
				
			}
			
			pw.close();
			br.close();
			client.close();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
		
		
		
	}
	
	

登录线程:

public class LoginThread extends Thread{


	Socket client;
	Properties userPro;

	public LoginThread(Socket client, Properties userPro) {
		super();
		this.client = client;
		this.userPro = userPro;
	}



	@Override
	public void run() {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
			String message = br.readLine();
			String[] ss = message.split("#");
			String name = ss[0];
			String password = ss[1];
			
			if(userPro.containsKey(name)){
				if(userPro.get(name).equals(password)){
					pw.println("登录成功");
					pw.flush();
				}
				else{
					pw.println("密码输入错误");
					pw.flush();
				}
				
			}else{
				pw.println("用户名出输入错误");
				pw.flush();				
			}
			
			pw.close();
			br.close();
			client.close();
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		}
		
		
		
	}

启动页面2:

public class TestMnue {

	public static void main(String[] args) throws Exception {
		TestSocket ts = new TestSocket();
		int n;
		do{
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入操作编号:1.注册2.登录 0.退出");
			n = sc.nextInt();
			switch (n) {
			case 1:
				ts.reg();
				break;
			case 2:
				ts.login();
				break;
				
			default:
				break;
			}
	
		}while(n != 0);
		
		System.out.println("欢迎下次再来");
	}

}

客户端:

public class TestSocket {
	public void reg() throws Exception{
		Socket client = new Socket("192.168.1.104",2000);
		SocketRegThread t = new SocketRegThread(client);
		
		t.start();
		t.join();

	}
	public void login() throws Exception{
		Socket client = new Socket("192.168.1.104",4000);
		SocketLoginThread t2 = new SocketLoginThread(client);
		
		t2.start();
		t2.join();

	}
	
	
	
	
}

注册线程:

public class SocketRegThread extends Thread {
	Socket client;
	
	public SocketRegThread(Socket client) {
		super();
		this.client = client;
	}

	public void run() {
		
		Scanner sc= new Scanner(System.in);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
			System.out.println("请输入注册用户名:");
			String name = sc.next();
			System.out.println("请输入密码:");
			String password = sc.next();
			String combined = name + "#" + password;
			
			pw.println(combined);
			pw.flush();
			
			String message = br.readLine();
			System.out.println(message);
			
			pw.close();
			br.close();
			client.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}

登录线程:

public class SocketLoginThread extends Thread {
Socket client;
	
	public SocketLoginThread(Socket client) {
		super();
		this.client = client;
	}

	public void run() {
		
		Scanner sc= new Scanner(System.in);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream(),"UTF-8"));
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(client.getOutputStream(),"UTF-8"));
			System.out.println("请输入用户名:");
			String name = sc.next();
			System.out.println("请输入密码:");
			String password = sc.next();
			String combined = name + "#" + password;
			
			pw.println(combined);
			pw.flush();
			
			String message = br.readLine();
			System.out.println(message);
			
			pw.close();
			br.close();
			client.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 14
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值