📄 chatserver.java
字号:
package service;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
public class ChatServer extends JFrame implements ActionListener {
public static int r_number;//注册用户数量
public static Personinf[] register_inf=new Personinf[100];
public static Hashtable peopleList=new Hashtable();//存放与各聊天者客户端通信的服务器线程散列表
private static JTextArea showmessage;
public static int search(Personinf[] a,String b,int c){//在a中寻找b,寻找c个单元,如果没找到返回-1
for(int i=0;i<c;i++){
if(a[i].ID.equals(b)) return i;
}
return -1;
}
public ChatServer(){
super("飞腾服务器");
setSize(300,300);
setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane=getContentPane();
contentPane.setLayout(new BorderLayout());
JPanel panel1=new JPanel();
panel1.setBackground(Color.LIGHT_GRAY);
panel1.setLayout(new GridLayout(1,3));
JButton closeButton=new JButton("关闭");//直接关闭服务器会自动保存用户资料
closeButton.setBackground(Color.WHITE);
closeButton.addActionListener(this);
JButton saveButton=new JButton("保存");//保存服务器上的用户资料
saveButton.setBackground(Color.WHITE);
saveButton.addActionListener(this);
JButton flushButton=new JButton("刷新");//清楚服务器上所有用户资料
flushButton.setBackground(Color.WHITE);
flushButton.addActionListener(this);
panel1.add(closeButton);
panel1.add(saveButton);
panel1.add(flushButton);
contentPane.add(panel1,BorderLayout.NORTH);
showmessage=new JTextArea(5,5);
showmessage.setBackground(Color.WHITE);
showmessage.setEditable(false);
showmessage.setLineWrap(true);
JScrollPane showscrolled=new JScrollPane(showmessage);
showscrolled.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(showmessage,BorderLayout.CENTER);
try{
Font f = new Font("Tahoma",Font.PLAIN,11);
UIManager.put("TextField.font", f);
UIManager.put("Label.font", f);
UIManager.put("ComboBox.font",f);
UIManager.put("MenuBar.font",f);
UIManager.put("Menu.font",f);
UIManager.put("ToolTip.font",f);
UIManager.put("MenuItem.font",f);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
catch(Exception e){}
SwingUtilities.updateComponentTreeUI(closeButton);
SwingUtilities.updateComponentTreeUI(saveButton);
SwingUtilities.updateComponentTreeUI(flushButton);
SwingUtilities.updateComponentTreeUI(panel1);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
this.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
try{
ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream("r_numberdatafile"));
r_number=inputStream.readInt();
}
catch(FileNotFoundException e){
r_number=0;
showmessage.append("没有找到注册用户数量文件"+"\n");
}
catch(IOException e){
r_number=0;
showmessage.append("读入注册用户数量文件异常"+"\n");
}
try{
ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream("serverdatafile"));
register_inf=(Personinf[])inputStream.readObject();
inputStream.close();
}
catch(ClassNotFoundException e){
showmessage.append("没有找到类"+"\n");
}
catch(FileNotFoundException e){
showmessage.append("没有找到用户资料文件"+"\n");
}
catch(IOException e){
showmessage.append("读入用户资料文件异常"+"\n");
}
}
public void actionPerformed(ActionEvent e){
if(e.getActionCommand().equals("关闭")||e.getActionCommand().equals("保存")){
try{
ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("r_numberdatafile"));
outputStream.writeInt(r_number);
outputStream.close();
}
catch(FileNotFoundException ee){
showmessage.append("没有找到注册用户数量文件"+"\n");
}
catch(IOException ee){
showmessage.append("写入注册用户数量异常"+"\n");
}
try{
for(int i=0;i<r_number;i++){
register_inf[i].online=false;
}
ObjectOutputStream outputStream=
new ObjectOutputStream(new FileOutputStream("serverdatafile"));
outputStream.writeObject(register_inf);
outputStream.close();
System.exit(0);
}
catch(FileNotFoundException ee){
showmessage.append("没有找到保存用户资料文件"+"\n");
}
catch(IOException ee){
showmessage.append("写入用户资料异常"+"\n");
}
}
else{
try{
ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("r_numberdatafile"));
outputStream.close();
}
catch(IOException ee){
showmessage.append("刷新注册用户数量异常"+"\n");
}
try{
ObjectOutputStream outputStream=
new ObjectOutputStream(new FileOutputStream("serverdatafile"));
outputStream.close();
}
catch(IOException ee){
showmessage.append("刷新用户资料异常"+"\n");
}
System.exit(0);
}
}
public static void main(String args[])throws IOException{
ServerSocket server=null;
Socket you=null;
DataInputStream input;
DataOutputStream output;
ChatServer service=new ChatServer();
service.setVisible(true);
while(true){
try{
server=new ServerSocket(5000);
}
catch(IOException e1){
service.showmessage.append("正在监听"+"\n");
}
try{
you=server.accept(); //建立和客户端的连接的套接字
InetAddress address=you.getInetAddress();
service.showmessage.append("用户的IP"+address+"\n");
}
catch(IOException e2){
service.showmessage.append("网络异常"+"\n");
}
if(you!=null){
input=new DataInputStream(you.getInputStream());
output=new DataOutputStream(you.getOutputStream());
String s=input.readUTF();
if(s.startsWith("注册")){//消息分别是ID,名称和密码。分别用":""!""@"分隔
if(r_number==100) output.writeUTF("注册失败,人员已满");
else{
String temp_ID=s.substring(s.indexOf(":")+1,s.indexOf("!"));
String temp_name=s.substring(s.indexOf("!")+1,s.indexOf("@"));
String temp_code=s.substring(s.indexOf("@")+1);
int v=search(register_inf,temp_ID,r_number);
if(v==-1) {
register_inf[r_number]=new Personinf(temp_ID,temp_name,temp_code,false);
r_number++;
output.writeUTF("注册成功");
Enumeration enumer=peopleList.elements();//获取所有的与客户端通信的服务器线程
while(enumer.hasMoreElements()){
Server_thread th=(Server_thread)enumer.nextElement();//将当前注册用户通知给所有在线用户
th.outputstream.writeUTF("注册者:"+register_inf[r_number-1].name+"("+register_inf[r_number-1].ID+")");
}
}
else output.writeUTF("该号码已被注册,请输入其他号码");
}
}
else if(s.startsWith("登录")){ //如果用户提交了ID和密码,分隔符为"!";
String temp_ID=s.substring(s.indexOf(":")+1,s.indexOf("!"));
String temp_code=s.substring(s.indexOf("!")+1);
int v=search(register_inf,temp_ID,r_number);
if(v==-1) output.writeUTF("输入的号码不曾注册");
else if(!temp_code.equals(register_inf[v].code))output.writeUTF("密码错误");
else if(register_inf[v].online) output.writeUTF("登录失败,ID已经登录");
else{
output.writeUTF("登录成功:"+register_inf[v].name);
register_inf[v].online=true;
for(int i=0;i<register_inf[v].num_of_privateleavemessage;i++){
output.writeUTF("私人留言信息:"+register_inf[v].privateleavemessage[i]);
register_inf[v].privateleavemessage[i]=null;
register_inf[v].num_of_privateleavemessage--;
}
for(int j=0;j<register_inf[v].num_of_publicleavemessage;j++){
output.writeUTF("公共留言信息:"+register_inf[v].publicleavemessage[j]);
register_inf[v].publicleavemessage[j]=null;
register_inf[v].num_of_publicleavemessage--;
}
service.showmessage.append("登录成功"+"\n");
Server_thread peopleThread=new Server_thread(you,register_inf[v].ID,register_inf[v].name,v);
peopleThread.start();
}
}
}
else continue;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -