用基于TCP的网络编程实现多个用户登录功能的简单模拟

Person类:

用来存储每个对象的用户名和密码信息

import java.io.Serializable;

public class Person implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private String name;
	private String password;
	
	public Person(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}

	//setter和getter方法
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
}

客户端类:

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

public class UploadClient {

	public static void main(String[] args) throws IOException {
		//提醒并接收用户输入
		Scanner sc=new Scanner(System.in);
		System.out.print("用户名:");
		String name=sc.next();
		System.out.print("密码:");
		String password=sc.next();
		
		
		//连接服务器利用Socket创建输出流,并将对象输出到服务器
		Socket s=new Socket("192.168.51.179",3602);
		OutputStream os = s.getOutputStream();
		ObjectOutputStream oos=new ObjectOutputStream(os);
		oos.writeObject(new Person(name,password));
		
		
		//关闭输出流
		s.shutdownOutput();
		
		//利用Socket创建输入流,接收服务器验证信息
		InputStream is = s.getInputStream();
		DataInputStream dis=new DataInputStream(is);
		System.out.print(dis.readUTF());
		
		//关流
		dis.close();
		is.close();
		s.close();	
		
	}
}

服务器类:

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

public class UploadServer {

	public static void main(String[] args)  {
		System.out.println("服务器接收数据!");
		//连接客户端利用Socket创建输入流并接收数据
		ServerSocket ss=null;
		Socket s =null;
		int count=0;//统计第几个用户登录
		try {
			//这个必须放在while循环外面,否则会报端口占用的错误
			ss = new ServerSocket(3602);
			while(true) {
				s = ss.accept();
				/*
				 * 每次有用户登入,则为该用户创建一个线程,启动该线程来验证用户的登录是否成功
				 */
				new Thread(new ServerThread(s)).start();
				System.out.println("第"+(count++)+"个用户在访问,IP地址是"+s.getInetAddress());
			}	
		} catch (IOException e) {
			e.printStackTrace();
		}
		/*
		 * 服务器要一直运行,所以不用关闭网络资源
		 */
	}

}

服务器线程类:

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

public class ServerThread implements Runnable {
	InputStream is=null;
	ObjectInputStream ois=null;
	OutputStream os=null ;
	DataOutputStream dos=null;
	Socket s;
	
	public ServerThread(Socket s) {
		this.s=s;
	}
	
	@Override
	public void run() {
		
		try {
			//通过Socket对象拿到输入流
			is = s.getInputStream();
			ois=new ObjectInputStream(is);
			Person p = (Person)ois.readObject();
				
			//关闭Socket输入流
			s.shutdownInput();
			
			//启动Socket输出流,将登陆成功或失败信息返回给客户端
			os = s.getOutputStream();
			dos=new DataOutputStream(os);
				
			//对接收到的数据进行判定
			if("lili".equals(p.getName())&&"123123".equals(p.getPassword())) {
				dos.writeUTF("登陆成功!");
			}else {
				dos.writeUTF("登陆失败!");
			}
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
				//关流
			try {
				s.shutdownOutput();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}		
		}

	}
}

猜你喜欢

转载自blog.csdn.net/pengzonglu7292/article/details/85092234
今日推荐