📄 serverframe.java
字号:
import java.net.*;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerFrame extends Frame
{
public static final int PORT = 4444;
HashMap user_map = new HashMap();
// 存放name,ClientConnection 参数对
private Vector user_online = new Vector();
// /存放在线用户名及其状态和积分
//如果要发消息给 name 用户,可以 ((ClientConnection)user_map.get(name)).out.println(/ 要发的内容 */);
public TextArea sStatus;
java.awt.List info;
private Button exit;
private Label lab;
ServerFrame()
{ this.setTitle(">>>五子棋联机服务器运行中<<<");
this.setResizable (false);
this.setLayout (null);
setSize(600,560);
setBackground(Color.DARK_GRAY);
setVisible(true);
sStatus=new TextArea();
sStatus.setSize (350,500);
sStatus.setLocation (2,28);
add(sStatus);
sStatus.setForeground (Color.white);
sStatus.setBackground (Color.DARK_GRAY);
sStatus.append("------- <<GAME SERVER HAE BEEN SET UP!!>>------"+"\n");
sStatus.append(">> WAITING FOR CONNECTION....."+"\n");
info=new java.awt.List();
info.setSize (250,470);
info.setLocation (352,58);
add(info);
info.setForeground (Color.white);
info.setBackground (Color.DARK_GRAY);
lab=new Label(" USER INFO:");
lab.setSize (250,30);
lab.setLocation (352,28);
add(lab);
lab.setForeground (Color.white);
lab.setBackground (Color.DARK_GRAY);
exit=new Button("Exit");
exit.setSize (600,30);
exit.setLocation (2,529);
add(exit);
exit.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void catchname(StringTokenizer st)
{
String name= st.nextToken();
String catchname=(String)user_map.get(name);
for(int i=0;i<user_online.toArray().length;i++)
{
st = new StringTokenizer((String)user_online.elementAt(i));
if(catchname.equals(st.nextToken()))
user_online.remove(i);
}
}
//用户n 向在线所有其他用户发送信息msg
private void broadcast(String n,String msg)
{
try
{
for(int i=0;i<user_online.toArray().length;i++)
{
StringTokenizer st = new StringTokenizer((String)user_online.elementAt(i));
String to = st.nextToken();
if(!n.equals(to))
send(to,msg);
}
}
catch(Exception ex)
{
}
}
//向用户n 发送信息msg
private void send(String n,String msg)
{
System.out.println("Sending message '"+msg+"' to "+n);
ClientConnection c=(ClientConnection)user_map.get(n);
c.out.println(msg);
c.out.flush();
}
//更新用户上下线信息
private void updateInfoArea(String name,boolean s_or_x)
{
if(s_or_x) sStatus.append (">>user: "+name+" has joined! "+"\n");
else sStatus.append(">>user: "+name+" has left!" +"\n" );
}
//动态显示在线用户及其状态
public void updateList()
{
info.removeAll();
for (int i=0;i<=user_online.size();i++)
{
try{
info.add((String)user_online.elementAt(i));
}catch(Exception e){}
}
}
//读取用户资料及积分
private void updateUserInfo(String name)
{
try
{
String score=new String();
File userfile=new File("d:\\users\\"+name+".txt");
BufferedReader bf=new BufferedReader(new FileReader(userfile) );
for(int i=0;i<2;i++)
{
score=bf.readLine();
}
userfile.delete();
File userfile1=new File("d:\\users\\"+name+".txt");
PrintStream ps=new PrintStream(new FileOutputStream( userfile1));
ps.println(name);
ps.println(score);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//对用户的注册要求进行分析并予以响应
public void scoreplus(String name)//对用户name的积分加1
{
int score=0;
try
{
File filescore=new File("d:\\users\\"+name+".txt");
FileReader frscore=new FileReader(filescore);
BufferedReader brscore=new BufferedReader(frscore);
String passwd=brscore.readLine();
score=Integer.parseInt(brscore.readLine());
score++;
PrintStream psscore=new PrintStream(new FileOutputStream(filescore));
psscore.println(passwd);
psscore.println(""+score);
}catch(Exception e)
{
e.printStackTrace();
}
}
public int getScore(String name)//获取用户name的积分
{
int score;
try{
File filescore=new File("d:\\users\\"+name+".txt");
FileReader frscore=new FileReader(filescore);
BufferedReader brscore=new BufferedReader(frscore);
String passwd=brscore.readLine();
score=Integer.parseInt(brscore.readLine());
return score;
}catch(Exception e)
{
e.printStackTrace();
}
return 0;
}
public void scoresub(String name)//将用户name的积分减一
{
int score=0;
try
{
File filescore=new File("d:\\users\\"+name+".txt");
FileReader frscore=new FileReader(filescore);
BufferedReader brscore=new BufferedReader(frscore);
String passwd=brscore.readLine();
score=Integer.parseInt(brscore.readLine());
score--;
PrintStream psscore=new PrintStream(new FileOutputStream(filescore));
psscore.println(passwd);
psscore.println(""+score);
}catch(Exception e)
{
e.printStackTrace();
}
}
public synchronized void do_reg(ClientConnection cc,StringTokenizer st)
//对注册事件作出反应 如果已经存在同名用户 跳出警告对话框
{
String name = st.nextToken();
String passwd = st.nextToken();
File filedir=new File("d:\\users");
boolean is_dirmk=filedir.mkdir();
try
{
File temp=new File("d:\\users\\"+name+".txt");
if(temp.exists())
{
cc.out.println(MsgType.REGFAIL);
System.out.println("file exists");
return;
}
else
{
try
{
File userfile =new File("d:\\users\\"+name+".txt");
userfile.createNewFile();
PrintStream ps=new PrintStream(new FileOutputStream( userfile));
ps.println (passwd);
ps.println (0);
}
catch(Exception e)
{
e.printStackTrace();
}
user_online.add(new String(name+" free"+" "+getScore(name)));
//增加新登陆或者注册用户
user_map.put(name, cc);
//将用户及其连接放入user_map
System.out.println(user_map);
//向name用户发送注册成功消息
send(name,String.valueOf(MsgType.REGOK));
broadcast(name,new String(MsgType.LOGON+" "+name+" "));
//用户name告诉所有其他在线用户他已经上线
String list = new String();
for(int i=0;i<user_online.toArray().length;i++)
{
list += (String)user_online.elementAt(i);
list += " ";
}
send(name,new String(MsgType.SENDLIST+" "+list));
updateList();
updateInfoArea(name,true);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public boolean isuser_online(String name)//判断用户name是否在线
{
boolean a=false;
String n=name;
System.out.println(n);
for(int i=0;i<user_online.toArray().length;i++)
{
StringTokenizer st=new StringTokenizer((String)user_online.elementAt(i));
String online_name= st.nextToken();
//System.out.println(online_name);
if(n.equals(online_name))
a=true;
}
//System.out.println(a+"dasdasdasdasdas");
return a;
}
public synchronized void do_logon(ClientConnection cc,StringTokenizer st)
//对用户登录事件作出回应 如果不存在该用户或者密码错误 则跳出对话框
{
String name = st.nextToken();
String passwd = st.nextToken();
String as=new String();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -