📄 serve.java
字号:
String name1_message = str.substring(str.indexOf("#")+1);
name1 = name1_message.substring(0,name1_message.indexOf("#"));
if(infor_table.containsKey(name1)) //如果用户在线
{
String message = name1_message.substring(name1_message.indexOf("#")+1);
Socket temp_socket = (Socket)infor_table.get(name1);
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) //表示停止对某一用户的服务
{
name1 = str.substring(str.indexOf("#")+1);
if(infor_table.containsKey(name1)) //如果用户在线
{
Socket temp_socket = (Socket)infor_table.get(name1);
PrintWriter out3 = new PrintWriter (temp_socket.getOutputStream(),true); //连接要发送用户的套接字
out3.write("服务器已停止对你的服务\n");
out3.flush();
temp_socket.close();
System.out.println("停止对"+name1+"的服务");
}
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 name1 = 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 + " (用户名#密码)");
//(server.tal).append("有IP申请注册新用户 :" + s + " (用户名#密码)\n");
int index_temp = s.indexOf("#");
if(name1_exist(s) == 0) ////////////////////////////////////
{
System.out.println("该用户注册成功 "+"\n");
//tal.append("该用户注册成功 "+"\n");
user_list.add(s); //增加到用户链表中
PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter("user.txt",true)));
out.close();
out1.write("1\n");
out1.flush();
}
else
{
System.out.println("该用户注册失败 "+"\n");
//tal.append("该用户注册失败 "+"\n");
out1.write("2\n");
out1.flush();
}
}
else //如果是用户登录
{
name1 = s.substring(0,s.indexOf("#"));
if(user_legal(s) == 1) //如果用户合法
{
System.out.println("该用户登录成功 :" + s + " (用户名#密码)"+"\n");
//tal.append("该用户登录成功 :" + s + " (用户名#密码)"+"\n");
out1.write("yes\n");
out1.flush();
out1.write(" 服务器:谢谢大家的测试,使用时首先点击查找在线用户,然后在左下角输入用户名,再在下拉框中选择该用户,然后就可以发短信了,祝大家开心!! "+"\n");
out1.flush();
infor_table.put(name1, socket); //把登录的用户名和套接字加入哈希表
break;
}
else //用户不合法,发送信息
{
System.out.println("该用户登录失败 :"+s+" (用户名#密码)"+"\n");
//tal.append("该用户登录失败 :"+s+" (用户名#密码)"+"\n");
out1.write("no\n");
out1.flush();
}
}
}
catch(IOException e) //表示用户突然断开连接
{
System.out.println("IP断开连接1 :"+socket+"\n");
//tal.append("IP断开连接1 :"+socket+"\n");
infor_table.remove(name1); //把用户从哈希表中去除
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception j){}
return;
}
catch(NullPointerException f) //表示用户突然断开连接
{
System.out.println("IP用户断开连接2 :"+socket+"\n");
//tal.append("IP用户断开连接2 :"+socket+"\n");
infor_table.remove(name1); //把用户从哈希表中去除
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("#");
name1 = str_temp.substring(0,i);
String message = str_temp.substring(i+1);
if(infor_table.containsKey(name1)) //如果要发的用户在线
{
Socket temp_socket1 = (Socket)infor_table.get(name1);
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+" 发送信息给 "+name1+" : "+message);
}
else
if(name1.compareTo("对所有人")!=0) //没找到用户
{
Date dat = new Date();
String da = dat.toString();
System.out.println(temp1+" 发送信息(失败)给 "+name1+"("+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(name1);
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(name1);
list.remove(socket);
try{
socket.close(); //关闭套接字,并且结束进程
}
catch(Exception h){}
return;
}
catch(NullPointerException f) //表示用户突然断开连接
{
System.out.println("用户下线2");
infor_table.remove(name1);
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 name1_exist(String s)
{
int index = s.indexOf("#");
int flag = 0;
String str_name1 = 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_name1)==0)
{
flag = 1;
break;
}
}
return flag;
}
} // class clientthread
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -