实操2:基于TCP协议的Socket编程 双向编程 (实现多个客户端同时登录服务器)

实操2:基于TCP协议的Socket编程 双向编程 (实现多个客户端同时登录服务器)
一、目录结构
1.首先新建两个工程clientProject客户端和serverProject服务器端工程
2.新建项目结构如图
在这里插入图片描述
在这里插入图片描述
二、编写代码
1.新建服务器端Server.java类和封装的User用户类和多用户启动的线程类
2.新建客户端Client.java类和封装的User用户类
客户端代码如下:

package com.bjsxt.entity;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
	public static void main(String[] args) throws IOException {
		//1.创建Socket对象,用于连接服务器
		Socket client=new Socket("localhost",11000);
		//2.获取输出流(对象流)
		ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
		//3.创建User对象
		//User u=new User("bjsxt","bjsxt");
		User  u=getUser();
		//4.user对象发送到服务器
		oos.writeObject(u);
		//5.获取输入流(数据流)
		DataInputStream dis=new DataInputStream(client.getInputStream());
		System.out.println(dis.readUTF());
		//6.关闭流
		if(dis!=null){
			dis.close();
		}
		if(oos!=null){
			oos.close();
		}
		if(client!=null){
			client.close();
		}
	}
	public static User getUser(){
		Scanner input=new Scanner(System.in);
		System.out.println("请输入用户名:");
		String userName=input.next();
		System.out.println("请输入密码:");
		String password=input.next();
		//封装成User对象
		return new User(userName,password);
	}
}
package com.bjsxt.entity;

import java.io.Serializable;

public class User implements Serializable{/**
	 * 
	 */
	private static final long serialVersionUID = -2456098105219660370L;
	//用于封装用户名和密码
	
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	public User() {
		super();
	}
	
}

服务器端代码如下:

package com.bjsxt.entity;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import com.bjsxt.thread.ServerThread;
/**
 * @author 祁蒙恩
 *	实操2:基于TCP协议的Socket编程  双向编程  (实现多个客户端同时登录服务器)
 */

public class Server {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		System.out.println("==========服务器端已经启动================");
		//1.创建ServerSocket对象
		ServerSocket server=new ServerSocket(11000);
		while(true){
			Socket socket=server.accept();
			ServerThread st=new ServerThread(socket);
			new Thread(st).start();
		}
	}
	//
}

package com.bjsxt.entity;

import java.io.Serializable;

public class User implements Serializable{/**
	 * 
	 */
	private static final long serialVersionUID = -2456098105219660370L;
	//用于封装用户名和密码
	
	private String userName;
	private String password;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public User(String userName, String password) {
		super();
		this.userName = userName;
		this.password = password;
	}
	public User() {
		super();
	}
	
}

package com.bjsxt.thread;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

import com.bjsxt.entity.User;

public class ServerThread implements Runnable {
	private Socket socket;//成员变量
	public ServerThread(Socket socket){
		this.socket=socket;
	}
	
	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName()+"请求登录!!");
		//2.获取输入流(对象流)
		ObjectInputStream ois=null;
		//4.获取输出流(数据流)
		DataOutputStream dos=null;
		try {
			ois = new ObjectInputStream(socket.getInputStream());
					User user=(User) ois.readObject();
					System.out.println(socket.getInetAddress().getHostAddress()+"请求登录:用户名"+user.getUserName()+"\t密码"+user.getPassword());
					//3.对用户名和密码进行验证
					String str="";
					if("Lizr".equals(user.getUserName())&&"123".equals(user.getPassword())){
						str="登录成功!";
					}else{
						str="登录失败";
					}
					dos = new DataOutputStream(socket.getOutputStream());
					dos.writeUTF(str);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭流
			if(dos!=null){
				try {
					dos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(socket!=null){
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
				
	}

}

运行结果:
0在这里插入图片描述
在这里插入图片描述客户端登录的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值