网络编程基础-----使用Scoket编程实现服务器端登录

利用端口实现数据的录入读取,对于相同的用户名数据不允录入。

public class Demo {
	public static void main(String[] args) {
		RegServerThread t=new RegServerThread();//创建该对象
		t.start();//运行其线程		
		Client c=new Client();//创建其对象
		c.showMenu();//调用其方法
	}
}

打印菜单栏

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
	Scanner input=new Scanner(System.in);
	public void showMenu() {
		while(true) {
			System.out.println("1:注册\t\t2.退出\t");
			String chose=input.next();
			switch(chose) {
			case "1":
			//调用注册的方法
				doReg();
				break;
			case "2":
				System.out.println("欢迎下次光临");
				break;
			default:
				System.out.println("没有该选项");
				break;
			}
		}
	}
	public void doReg() {
		Socket s=null;
		BufferedReader br=null;
		BufferedWriter bw =null;
		try {
			//请求连接到服务器
			s=new Socket("localhost",9998);
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
			//发送数据过去
			User u=inputUserInfo();
			String regRequst=u.toString();
			bw.write(regRequst);
			bw.newLine();
			bw.flush();		
			//接收结果
			String response=br.readLine();
			System.out.println("结果为:"+response);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bw.close();
				br.close();
				s.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public User inputUserInfo() {//获取用户信息
		System.out.println("请输入你的ID");
		String   id=input.next();
		System.out.println("请输入你的用户名");
		String   name=input.next();
		System.out.println("请输入你的密码");
		String   pwd=input.next();
		System.out.println("请输入你的年龄");
		int age =input.nextInt();
		System.out.println("请输入你的分数");
		double score =input.nextDouble();
		return new User(id,name,pwd,age,score);		
	}		
}

配置文件工具类型

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class ProUtils {
	//设置配置文件位置
	public static final String PATH="D:\\1\\user.properties";
	public static void savePro(String key,String value) {
		//创建properties对象
		Properties pro=new Properties();
		try {
			//设值
			pro.setProperty(key, value);
			//使用输出流保存数据
			pro.store(new FileOutputStream(PATH,true),"");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	public static Properties getPro() {
		Properties pro =new Properties();
		try {
			//读取配置文件并将其返回
			pro.load(new FileInputStream(PATH));
			return pro;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;		
	}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;

public class RegServerThread extends Thread{
	@Override
	public void run() {
		ServerSocket ss=null;
		Socket s=null;
		BufferedReader br=null;
		BufferedWriter bw =null;
		try {
			ss=new ServerSocket(9998);//端口
			s=ss.accept();//接受客户端的请求
			//获取用户输入的数据
			br=new BufferedReader(new InputStreamReader(s.getInputStream()));
			bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
			//读取用户输入的数据
			String userInfo=br.readLine();
			String ids=userInfo.substring(1,userInfo.length()-1).split(",")[0];
			String idValue=ids.split(":")[1];
			//判断该用户名是否注册过
			Properties pros=ProUtils.getPro();
			String msg="";
			if(pros.containsKey(idValue)) {
				msg="该用户已经注册";
			}else {
				ProUtils.savePro(idValue, userInfo);
				msg="用户注册成功";
			}
			//把结果给用户
			bw.write(msg);
			bw.flush();		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bw.close();
				br.close();
				s.close();
				ss.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
public class User {
	private String id;
	private String name;
	private String pwd;
	private int age ;
	private double score;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public User(String id, String name, String pwd, int age, double score) {
		super();
		this.id = id;
		this.name = name;
		this.pwd = pwd;
		this.age = age;
		this.score = score;
	}
	public User() {
		super();
	}
	@Override
	public String toString() {
		return "{id:" + id + ", name:" + name + ", pwd:" + pwd + ", age:" + age + ", score:" + score + "}";
	}
}

这几天的状态就是,快,多。有点吃力了,现在的感觉就是。
但是其实自己还是有很多的进步空间,学习的节奏上把握的不是很好。
现在要注意的特别是复习,以及空余时间的利用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值