📄 serve.java
字号:
//客户端发送的信息定义一套功能码
// 1#表示注册
//2#表示登录
//3#表示请服务器转发发送信息
//4#表示查找好友
//5#表示加为好友
//6#表示下线
import java.net.*;
import java.io.*;
import java.util.*;
class serve
{
static LinkedList user_list ; //用户名和密码的链表
static Hashtable infor_table; //用户名和套接字的映射
static LinkedList list; //套接字的链表
static ServerSocket serversocket; //服务器的套接字
public static void main(String[] args)
{
user_list = new LinkedList(); //用户名和密码的链表
infor_table = new Hashtable(); //用户名和套接字的映射
try{
System.out.println("请输入端口号(1024~9999)");
BufferedReader port_in = new BufferedReader(new InputStreamReader(System.in));
String port_in_str = port_in.readLine();
serversocket = new ServerSocket(Integer.parseInt(port_in_str));
System.out.println("你已经开启了此服务器 : IP:"+(InetAddress.getLocalHost()).getHostAddress()+" 端口: "+port_in_str);
list = new LinkedList(); //套接字的链表
create_user(user_list); //建立用户链表
System.out.println("当前总共注册人数: "+user_list.size());
serverthread server_thread = new serverthread();
server_thread.start();
System.out.println("\n/************服务器操作说明**********************************************/");
System.out.println("输入 : 1# , 服务器退出服务");
System.out.println("输入 : 2# , 返回所有用户名");
System.out.println("输入 : 3#用户名#信息 , 给某一用户发消息");
System.out.println("输入 : 4# , 给所有用户发信息");
System.out.println("输入 : 5#用户名 , 停止对某一用户的服务");
System.out.println("/************************************************************************/\n");
while(true)
{
Socket insocket = serversocket.accept();
list.add(insocket);
System.out.println("有电脑连入服务器 : "+insocket);
clientthread c = new clientthread(insocket,list,user_list,infor_table);
c.start();
}
}
catch(Exception e){}
}
public static void create_user(LinkedList user_list) //把电脑上的user.txt载入链表中
{
try{
BufferedReader in = new BufferedReader(new FileReader("user.txt"));
String s ;
while((s = in.readLine())!=null)
{
if(s.compareTo("")!=0)
{
user_list.add(s);
}
}
in.close();
}
catch(Exception h){}
}
public static class serverthread extends Thread //服务器控制台输入线程
{
String str,fun,name;
serverthread ()
{
}
public void run()
{
while(true)
{
try{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //接收控制台输入
str = input.readLine();
fun = str.substring(0,str.indexOf("#"));
System.out.println( "/*****************************************************/");
if(fun.compareTo("1")==0) //表示退出
{
Socket temp;
ListIterator lit;
for(lit = list.listIterator();lit.hasNext(); )
{
temp = (Socket)lit.next();
PrintWriter out = new PrintWriter (temp.getOutputStream(),true);
out.write("服务器已经停止服务"+"\n");
out.flush();
}
for(lit = list.listIterator();lit.hasNext(); )
{
temp = (Socket)lit.next();
temp.close();
}
serversocket.close();
System.out.println("退出程序");
System.exit(0);
}
if(fun.compareTo("2")==0) //表示返回所有用户名
{
System.out.println("所有用户名字");
for(Enumeration e = infor_table.keys();e.hasMoreElements(); )
{
System.out.println( (String)e.nextElement());
}
}
if(fun.compareTo("3")==0) //表示给某一用户发消息
{
String name_message = str.substring(str.indexOf("#")+1);
name = name_message.substring(0,name_message.indexOf("#"));
if(infor_table.containsKey(name)) //如果用户在线
{
String message = name_message.substring(name_message.indexOf("#")+1);
Socket temp_socket = (Socket)infor_table.get(name);
PrintWriter out3 = new PrintWriter (temp_socket.getOutputStream(),true); //连接要发送用户的套接字
Date dat = new Date();
String da = dat.toString();
out3.write("(服务器发送给你的信息) ["+da.substring(10,19)+"] :"+" "+message+"\n");
out3.flush();
System.out.println("发送成功");
}
else
{
System.out.println("该用户不存在");
}
}
if(fun.compareTo("4")==0) //表示给所有用户发信息
{
System.out.println("已给所有用户发送消息");
String message = str.substring(str.indexOf("#")+1);
Socket temp;
ListIterator lit;
for(lit = list.listIterator();lit.hasNext(); )
{
temp = (Socket)lit.next();
PrintWriter out = new PrintWriter (temp.getOutputStream(),true);
out.write("(服务器发送给所有人信息): "+message+"\n");
out.flush();
}
}
if(fun.compareTo("5")==0) //表示停止对某一用户的服务
{
name = str.substring(str.indexOf("#")+1);
if(infor_table.containsKey(name)) //如果用户在线
{
Socket temp_socket = (Socket)infor_table.get(name);
PrintWriter out3 = new PrintWriter (temp_socket.getOutputStream(),true); //连接要发送用户的套接字
out3.write("服务器已停止对你的服务\n");
out3.flush();
temp_socket.close();
System.out.println("停止对"+name+"的服务");
}
else //否则用户不在线
{
System.out.println("该用户不存在");
}
}
if(fun.compareTo("6")==0)
{
System.out.println(list);
}
System.out.println( "/*****************************************************/\n");
}
catch(Exception e)
{
System.out.println("exception = "+e);
}
}
}
} //class serverthread ends
} //class serve ends
class clientthread extends Thread //处理客户端的线程类
{
public Socket socket;
public String str;
public LinkedList list;
public LinkedList user_list;
public Hashtable infor_table;
public clientthread(Socket socket,LinkedList list,LinkedList user_list,Hashtable infor_table )
{
this.socket = socket;
this.list = list;
this.user_list = user_list;
this.infor_table = infor_table;
}
public void run()
{
String name = new String();
//////用户名登录操作
while(true) //直到输入正确才跳出循环
{
try{
BufferedReader in1 = new BufferedReader (new InputStreamReader(socket.getInputStream()));
PrintWriter out1 = new PrintWriter (socket.getOutputStream(),true); //用来接收用户登录处理
String user = in1.readLine();//读取"功能码#用户#密码"字符串
int index = user.indexOf("#");
String str_fun = user.substring(0,index);
String s = user.substring(index+1); // "用户#密码"字符串
if(str_fun.compareTo("1")==0) // 如果是标识注册新用户
{
System.out.println("有IP申请注册新用户 :" + s + " (用户名#密码)");
int index_temp = s.indexOf("#");
if(name_exist(s) == 0) ////////////////////////////////////
{
System.out.println("该用户注册成功 "+"\n");
user_list.add(s); //增加到用户链表中
PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("user.txt",true)));
out.append(s+"\r\n"); // 写到用户文件中
out.close();
out1.write("1\n");
out1.flush();
}
else
{
System.out.println("该用户注册失败 "+"\n");
out1.write("2\n");
out1.flush();
}
}
else //如果是用户登录
{
name = s.substring(0,s.indexOf("#"));
if(user_legal(s) == 1) //如果用户合法
{
System.out.println("该用户登录成功 :" + s + " (用户名#密码)"+"\n");
out1.write("yes\n");
out1.flush();
out1.write(" 服务器:谢谢大家的测试,使用时首先点击查找在线用户,然后在左下角输入用户名,再在下拉框中选择该用户,然后就可以发短信了,祝大家开心!! "+"\n");
out1.flush();
infor_table.put(name, socket); //把登录的用户名和套接字加入哈希表
break;
}
else //用户不合法,发送信息
{
System.out.println("该用户登录失败 :"+s+" (用户名#密码)"+"\n");
out1.write("no\n");
out1.flush();
}
}
}
catch(IOException e) //表示用户突然断开连接
{
System.out.println("IP断开连接1 :"+socket+"\n");
infor_table.remove(name); //把用户从哈希表中去除
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception j){}
return;
}
catch(NullPointerException f) //表示用户突然断开连接
{
System.out.println("IP用户断开连接2 :"+socket+"\n");
infor_table.remove(name); //把用户从哈希表中去除
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception j){}
return;
}
}
String temp1 = new String(); //返回发送信息的用户名字
for(Enumeration e = infor_table.keys();e.hasMoreElements(); )
{
temp1 = (String)e.nextElement();
if(infor_table.get(temp1)==socket)
{
break;
}
}
try{
while(true) //服务器开始接收客户端发送的信息,并根据功能码处理
{
BufferedReader in2 = new BufferedReader (new InputStreamReader(socket.getInputStream()));
PrintWriter out2 = new PrintWriter (socket.getOutputStream(),true); //用来接收用户登录处理
str = in2.readLine();
System.out.println( "/*****************************************************/");
System.out.println( "服务器接收到 "+temp1+" 发的信息: "+str);
System.out.println( "服务器处理结果 :");
int index = str.indexOf("#");
String function = str.substring(0,index);
if(function.compareTo("3")==0) //3 表示请服务器转发信息
{
String str_temp = str.substring(index+1);
int i= str_temp.indexOf("#");
name = str_temp.substring(0,i);
String message = str_temp.substring(i+1);
if(infor_table.containsKey(name)) //如果要发的用户在线
{
Socket temp_socket1 = (Socket)infor_table.get(name);
String a1 = temp_socket1.toString();
PrintWriter out3 = new PrintWriter (temp_socket1.getOutputStream(),true); //连接要发送用户的套接字
Date dat = new Date();
String da = dat.toString();
out3.write(temp1+"发送信息给你 ("+da.substring(10,19)+") :"+" "+message+"\n");
out3.flush();
System.out.println(temp1+" 发送信息给 "+name+" : "+message);
}
else
if(name.compareTo("对所有人")!=0) //没找到用户
{
Date dat = new Date();
String da = dat.toString();
System.out.println(temp1+" 发送信息(失败)给 "+name+"("+da.substring(10,19)+")"+" : "+message);
PrintWriter out3 = new PrintWriter (socket.getOutputStream(),true); //连接要发送用户的套接字
out3.write("该信息发送失败"+"\n");
out3.flush();
}
else //发送相信给所有人
{
Socket temp;
ListIterator lit;
for(lit = list.listIterator();lit.hasNext(); )
{
temp = (Socket)lit.next();
PrintWriter out = new PrintWriter (temp.getOutputStream(),true);
out.write("("+temp1+"发送给所有人信息): "+message+"\n");
out.flush();
}
}
} //if(3)ends
if(function.compareTo("4")==0) //4 表示查找在线好友
{
System.out.println( temp1+"请求查找所有在线用户");
out2.write(" 在线用户:"+"\n");
out2.flush();
for(Enumeration e = infor_table.keys();e.hasMoreElements(); )
{
out2.write((String)e.nextElement()+"\n");
out2.flush();
}
}
if(function.compareTo("6")==0)
{
System.out.println(temp1+"用户断开连接");
infor_table.remove(name);
list.remove(socket);
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception h){}
return;
}
System.out.println( "/*****************************************************/");
}// while ends
} //try ends
catch(IOException e)
{
System.out.println("用户下线1");
infor_table.remove(name);
list.remove(socket);
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception h){}
return;
}
catch(NullPointerException f) //表示用户突然断开连接
{
System.out.println("用户下线2");
infor_table.remove(name);
list.remove(socket);
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception h){}
return;
}
} //run ends
public int user_legal(String p) //判断该用户名密码是否合法
{
int flag = 0;
String temp;
ListIterator lit;
for(lit = user_list.listIterator();lit.hasNext(); )
{
temp = (String)lit.next();
if(temp.compareTo(p)==0)
{
flag = 1;
break;
}
}
if(flag == 1)
{
return 1;
}
else
{
return 0;
}
}
public int name_exist(String s)
{
int index = s.indexOf("#");
int flag = 0;
String str_name = s.substring(0,index);
ListIterator lit;
String temp;
for(lit = user_list.listIterator();lit.hasNext(); )
{
temp = (String)lit.next();
int index2 = temp.indexOf("#");
temp = temp.substring(0,index2);
if(temp.compareTo(str_name)==0)
{
flag = 1;
break;
}
}
return flag;
}
} // class clientthread
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -