需求:客户端通过键盘录入用户名,服务端对这个用户名进行校验,如果该用户存在,在服务端显示XXX,已登陆,并在客户端显示XXX,欢迎光临。如果该用户不存在,在服务端显示XXX,尝试登陆,并在客户端显示XXX,该用户不存在,最多就登陆三次。本例将数据库简化为文本文件,存储用户名信息。
代码如下:
import java.net.*;
import java.io.*;
class TCPserver5{
static class user_thread implements Runnable{
private Socket s;
public user_thread(Socket s){
this.s=s;
}
public void run(){
BufferedReader fbr=null;
BufferedReader br=null;
BufferedWriter bw=null;
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(IOException e1){
throw new RuntimeException(e1);
}
try{
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
}catch(IOException e2){
throw new RuntimeException(e2);
}
try{
fbr=new BufferedReader(new FileReader("user.txt"));
}catch(IOException e3){
throw new RuntimeException(e3);
}
String name=null,search=null;
boolean flag=false;
int count=0,i=1;
for(;i<=3;i++){
try{
name=br.readLine();
}catch(IOException e4){
throw new RuntimeException(e4);
}
try{
while((search=fbr.readLine())!=null){
if(search.equals(name)){
flag=true;
break;
}
}
}catch(IOException e5){
throw new RuntimeException(e5);
}
try{
if(flag){
bw.write(name+"欢迎光临");
bw.newLine();
bw.flush();
break;
}else{
bw.write("你已错误登陆"+i+"次");
bw.newLine();
bw.flush();
}
}catch(IOException e6){
throw new RuntimeException(e6);
}
}
if(i>3){
try{
bw.write("你已错误登陆三次,"+name+"该用户不存在,登陆失败!");
bw.newLine();
bw.flush();
}catch(IOException e7){
throw new RuntimeException(e7);
}
System.out.println("用户"+name+"尝试登陆");
}
try{
s.close();
}catch(IOException e8){
throw new RuntimeException(e8);
}
}
}
public static void main(String[] args){
ServerSocket ss=null;
try{
ss=new ServerSocket(18888);
}catch(SocketException e1){
throw new RuntimeException(e1);
}catch(IOException e2){
throw new RuntimeException(e2);
}
while(true){
Socket s=null;
try{
s=ss.accept();
}catch(IOException e4){
throw new RuntimeException(e4);
}
System.out.println("ip"+s.getLocalAddress().getHostAddress()+"......"+"已连接");
new Thread(new user_thread(s)).start();
}
}
}
import java.net.*;
import java.io.*;
class TCPclient5 {
public static void main(String[] args){
Socket s=null;
try{
s=new Socket(InetAddress.getLocalHost(),18888);
}catch(UnknownHostException e1){
throw new RuntimeException(e1);
}catch(IOException e2){
throw new RuntimeException(e2);
}
BufferedReader br=null;
try{
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(IOException e3){
throw new RuntimeException(e3);
}
BufferedWriter bw=null;
try{
bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
}catch(IOException e4){
throw new RuntimeException(e4);
}
BufferedReader br_in=null;
br_in=new BufferedReader(new InputStreamReader(System.in));
int i=0;
String info=null;
try{
for(;i<3;i++){
bw.write(br_in.readLine());
bw.newLine();
bw.flush();
info=br.readLine();
System.out.println(info);
if(info.contains("欢迎"))
break;
}
}catch(IOException e5){
throw new RuntimeException(e5);
}
if(i==3)
System.out.println("登陆失败");
try{
s.close();
}catch(IOException e6){
throw new RuntimeException(e6);
}
}
} |
|