📄 serve.java.bak
字号:
//客户端发送的信息定义一套功能码
// 1#表示注册
//2#表示登录
//3#表示请服务器转发发送信息
//4#表示查找好友
//5#表示加为好友
//6#表示下线
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class serve extends JFrame implements ActionListener
{
static LinkedList user_list ; //用户名和密码的链表
static Hashtable infor_table; //用户名和套接字的映射
static LinkedList list; //套接字的链表
static ServerSocket serversocket1; //服务器的套接字
JTextField f1;//服务器端口
JTextField f2;//用户名
JTextField f3;//服务器端口
JTextField f4;//用户名
JTextField f5;//用户名
JTextField f6;//用户名
JButton command1; //启动服务器
JButton command2;//关闭服务器
JButton command3; //显示所有用户名
JButton command4;//给某用户发信息
JButton command5; //给所有用户发信息
JButton command6;//踢掉某人
static JTextArea tal; //服务器信息部分
static String msg="";
static serverthread server_thread;
static boolean flag=false;
public static void main(String[] args)
{
user_list = new LinkedList(); //用户名和密码的链表
infor_table = new Hashtable(); //用户名和套接字的映射
list = new LinkedList(); //套接字的链表
try{
serve server_frame = new serve();
while(true)
{
if(flag){
Socket insocket = serversocket1.accept();
list.add(insocket);
msg+=("有电脑连入服务器 : "+insocket);
tal.append(msg);
clientthread c = new clientthread(insocket,list,user_list,infor_table);
c.start();
}
}
}catch(Exception e){
System.out.println(e);
System.out.println("此处为e");
}
}
public serve(){
Container c = getContentPane();
Container upc = new Container();
c.setLayout(new BorderLayout(5,5));
c.add(upc,BorderLayout.NORTH); //界面上部布局
upc.setLayout(new GridLayout(1,4,15,5));
upc.add(new JLabel("服务器端口"));
f1 = new JTextField("1000");
upc.add(f1);
command1 = new JButton("启动"); //连接服务器
command1.addActionListener(this);//事件监听
upc.add(command1);
command2 = new JButton("关闭"); //连接服务器
command2.addActionListener(this);//事件监听
upc.add(command2);
command2.setEnabled(false);
Container centerc = new Container(); //界面中部布局
c.add(centerc,BorderLayout.CENTER);
tal = new JTextArea();
tal.setEditable(false);
tal.setFont(new Font("宋体",0,15));
tal.setWrapStyleWord(true);
tal.setLineWrap(true);
tal.setCaretPosition(0);
JScrollPane scroll = new JScrollPane(tal,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.revalidate();
this.getContentPane().add(scroll);
Container downc = new Container(); //界面下部布局
c.add(downc,BorderLayout.SOUTH);
downc.setLayout(new GridLayout(4,5,2,5));
downc.add(new JLabel(" "));
downc.add(new JLabel(" "));
command3 = new JButton("显示所有用户");
command3.setEnabled(false);
command3.addActionListener(this);//事件监听
downc.add(command3);
downc.add(new JLabel(" "));
downc.add(new JLabel(" "));
downc.add(new JLabel("要对用户:")) ;
f3 = new JTextField();
downc.add(f3);
downc.add(new JLabel("说:"));
f4= new JTextField();
downc.add(f4);
command4 = new JButton("发送信息");
command4.setEnabled(false);
command4.addActionListener(this);//事件监听
downc.add(command4);
downc.add(new JLabel("对所有人说:"));
f5= new JTextField();
downc.add(f5);
downc.add(new JLabel(" "));
downc.add(new JLabel(" "));
command5 = new JButton("发送信息");
command5.setEnabled(false);
command5.addActionListener(this);//事件监听
downc.add(command5);
downc.add(new JLabel("踢掉用户:"));
f6= new JTextField();
downc.add(f6);
downc.add(new JLabel(" "));
downc.add(new JLabel(" "));
command6 = new JButton("踢!!!");
command6.setEnabled(false);
command6.addActionListener(this);//事件监听
downc.add(command6);
String str = "服务器";
(getFrames())[0].setTitle(str);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,700);
setLocation(400,400);
setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
String name1="";
try{
if(e.getSource()== command1) ////////////////连接服务器
{
command1.setEnabled(false);
command2.setEnabled(true);
String port_in_str = f1.getText();
//System.out.println(port_in_str);
serversocket1= new ServerSocket(Integer.parseInt(port_in_str));
//System.out.println("serversocket1=null 在响应后 "+serversocket1==null);
msg=("你已经开启了此服务器 : IP:"+(InetAddress.getLocalHost()).getHostAddress()+" 端口: "+port_in_str);
create_user(user_list); //建立用户链表
msg+=("\n当前总共注册人数: "+user_list.size());
server_thread = new serverthread();
server_thread.start();
tal.append(msg);
//System.out.println(msg);
flag=true;
command3.setEnabled(true);
command4.setEnabled(true);
command5.setEnabled(true);
command6.setEnabled(true);
}
if(e.getSource()== command2) ////////////////关闭服务器
{
flag=false;
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();
}
serversocket1.close();
tal.append("服务器退出程序");
System.exit(0);
}
if(e.getSource()== command3) ////////////////显示用户
{
tal.append("\n所有用户名字\n");
for(Enumeration ee = infor_table.keys();ee.hasMoreElements(); )
{
tal.append( (String)ee.nextElement()+"\n");
}
}
if(e.getSource()== command4) ////////////////对某用户说
{
name1=f3.getText();
if(infor_table.containsKey(name1)) //如果用户在线
{
String message = f4.getText();
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();
tal.append("发送成功\n");
}
else
{
tal.append("该用户不存在");
}
}
if(e.getSource()== command5) ////////////////对所有用户说
{
tal.append("已给所有用户发送消息\n");
String message = f5.getText();
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(e.getSource()== command6) ////////////////踢掉用户
{
name1 = f6.getText();
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();
tal.append("停止对"+name1+"的服务\n");
}
else //否则用户不在线
{
System.out.println("该用户不存在");
}
}
}catch(Exception d){
System.out.println("此处为d");
System.out.println(d); }
}
/*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();
serversocket1 = new ServerSocket(Integer.parseInt(port_in_str));
System.out.println(serversocket1);
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 = serversocket1.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,name1;
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();
}
serversocket1.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) //表示给某一用户发消息
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -