📄 chatserver.java
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.applet.*;
// class 1:server main class,服务器启动类
public class ChatServer extends Frame {
final static int DEFAULT_USERCOUNT=30;
final static int DEFAULT_PORT=10000;
final static String ICON_FILE="icon.gif";
int num_Line=0;
User[] users=null;
ServerSocket ss=null;
RoomGroup roomGroup=null;
int port=DEFAULT_PORT;
Image icon=null;
java.awt.TextArea message_TextArea;
java.awt.List users_List;
java.awt.Choice room_Choice;
java.awt.Label message_Label;
java.awt.Label room_Label;
java.awt.Label userList_Label;
java.awt.Button clean_Button;
java.awt.MenuBar mainMenuBar;
java.awt.Menu menu1;
java.awt.Menu menu3;
void initServer(int port) throws IOException {
users=new User[ChatServer.DEFAULT_USERCOUNT];
roomGroup=new RoomGroup
(ChatServer.DEFAULT_USERCOUNT,room_Choice,
this);
try{
ss=new ServerSocket(port,5);
message_TextArea.appendText
("\nserver socket init successed");
}catch(Exception e){
System.out.println(e);
message_TextArea.appendText
("\nserver socket init error"+e);
}
for(int i=0;i< users.length;i++) {
users[i]=new User(this,i);
}
roomGroup.setUsers(users);
show();
go();
message_TextArea.appendText("\nserver started");
}
ChatServer(int port) throws IOException {
icon=getToolkit().getImage(ICON_FILE);
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(icon,0);
try{
tracker.waitForAll();
}catch(Exception e){
System.out.println(e);
}
//{{INIT_CONTROLS
setLayout(null);
addNotify();
resize(insets().left + insets().right + 544,insets().top
+ insets().bottom + 355);
//$$ openFileDialog1.move(36,276);
setTitle("Chat & WhiteBoard Server 1.0");
setIconImage(icon);
//}}
message_TextArea = new java.awt.TextArea();
message_TextArea.reshape(insets().left + 12,
insets().top + 36,396,240);
add(message_TextArea);
users_List = new java.awt.List(5,false);
add(users_List);
users_List.reshape(insets().left + 432,insets().top
+ 144,76,110);
room_Choice = new java.awt.Choice();
add(room_Choice);
room_Choice.reshape(insets().left + 420,insets().top
+ 60,100,1);
message_Label = new java.awt.Label("message");
message_Label.reshape(insets().left + 36,insets().top
+ 12,100,24);
add(message_Label);
room_Label = new java.awt.Label("Chat room");
room_Label.reshape(insets().left + 420,insets().top
+ 14,100,24);
add(room_Label);
userList_Label = new java.awt.Label("user List");
userList_Label.reshape(insets().left + 420,insets().top
+ 108,100,28);
add(userList_Label);
clean_Button = new java.awt.Button("clean");
clean_Button.reshape(insets().left + 180,insets().top
+ 12,60,24);
add(clean_Button);
//{{INIT_MENUS
mainMenuBar = new java.awt.MenuBar();
menu1 = new java.awt.Menu("File");
menu1.add("Restart");
menu1.add("Exit");
mainMenuBar.add(menu1);
menu3 = new java.awt.Menu("Help");
mainMenuBar.setHelpMenu(menu3);
menu3.add("About");
mainMenuBar.add(menu3);
setMenuBar(mainMenuBar);
//$$ mainMenuBar.move(4,277);
this.port=port;
initServer(port);
}// constructor 1 ended
ChatServer() throws IOException{
this(ChatServer.DEFAULT_PORT);
}// contructor 2 ended
void cleanButton_Clicked(Event event) {
//{{CONNECTION
// Hide the Frame
message_TextArea.setText("");
//}}
}
void About_Action(Event event) {
//{{CONNECTION
// Action from About Create and show as modal
(new AboutDialog(this, true)).show();
//}}
}
void Exit_Action(Event event) {
//{{CONNECTION
// Action from Exit Create and show as modal
(new QuitDialog(this, true)).show();
//}}
}
void Restart_Action(Event e) {
try{
quit();
initServer(port);
}catch(Exception e1){
message_TextArea.appendText("\ninit server error");
}
}
public synchronized void show() {
move(50, 50);
super.show();
}
void roomChoice_Action(Event e) {
String roomName=null;
int roomID=0;
users_List.clear();
roomName=room_Choice.getSelectedItem();
roomID=roomGroup.getRoomID(roomName);
for(int i=0;i< users.length;i++) {
if ((users[i].logged)&&(users[i].belongRoom)
&&(users[i].roomID==roomID)) {
users_List.addItem(users[i].name);
}
}
}
public boolean handleEvent(Event event) {
if (event.id == Event.WINDOW_DESTROY) {
quit();
hide(); // hide the Frame
dispose(); // free the system resources
System.exit(0); // close the application
return true;
}
if (event.target == room_Choice && event.id
== Event.ACTION_EVENT) {
roomChoice_Action(event);
return true;
}
if (event.target == clean_Button && event.id
== Event.ACTION_EVENT) {
cleanButton_Clicked(event);
return true;
}
return super.handleEvent(event);
}
public boolean action(Event event, Object arg) {
if (event.target instanceof MenuItem) {
String label = (String) arg;
if (label.equalsIgnoreCase("About")) {
About_Action(event);
return true;
} else
if (label.equalsIgnoreCase("Exit")) {
Exit_Action(event);
return true;
} else
if (label.equalsIgnoreCase("Restart")) {
Restart_Action(event);
return true;
}
}
return super.action(event, arg);
}
public static void main(String args[]) {
try{
ChatServer chatServer=new ChatServer();
}catch(Exception e){
System.out.println(e);
}
}
public void go() {
for(int i=0;i< users.length;i++) {
users[i].start();
}
}// method 'go' ended
public void quit() {
for(int i=0;i< users.length;i++) {
if (users[i]!=null) users[i].destroy();
}
try{
ss.close();
ss=null;
room_Choice.removeAll();
users_List.clear();
message_TextArea.appendText
("\nserver socket closed");
}catch(IOException e){
System.out.println(e);
}
}// method 'quit' ended
} // class 'ChatServer' ended
//class 2:RoomGroup,服务器用的交谈室数据类
class RoomGroup {
Room[] rooms=null;
final static String DEFAULT_ROOM_SUBJECT="默认";
final static int DEFAULT_ROOM_ID=0;
Choice room_Choice=null;
java.util.List users_List=null;
User[] users=null;
ChatServer chatServer=null;
RoomGroup(int roomCount,Choice room_Choice,
ChatServer chatServer) {
this.chatServer=chatServer;
this.room_Choice=room_Choice;
this.users=users;
this.users_List=users_List;
rooms=new Room[roomCount];
try{
newRoom(DEFAULT_ROOM_SUBJECT);
}catch(Exception e){
System.out.println(e);
}
}// contructor 1 ended
void setUsers(User[] users) {
this.users=users;
}
void refreshList() {
chatServer.handleEvent(new Event(this,
Event.ACTION_EVENT, null));
}
public int getRoomID(String roomName){
for(int i=0;i< rooms.length;i++) {
if ((rooms[i]!=null)&&(rooms[i].getRoomName().equals
(roomName))) {
return i;
}
}
return 0;
}// method 'getRoomID' ended
public int newRoom(String subject) throws
TooManyRoomsException {
int i=0;
while((i< rooms.length)&&(rooms[i]!=null)) {
i++;
}
if (i< rooms.length) {
rooms[i]=new Room(subject,i);
room_Choice.addItem(subject);
room_Choice.repaint();
return i;
}else {
throw new TooManyRoomsException();
}
} // method 'newRoom' ended
public void join(int roomID,User user) throws
NoSuchRoomException {
try{
rooms[roomID].join(user);
room_Choice.select(getRoomName(roomID));
refreshList();
}catch(ArrayIndexOutOfBoundsException e){
throw new NoSuchRoomException();
}catch(NullPointerException e) {
throw new NoSuchRoomException();
}
} // method 'join' ended
public void quit(int roomID,User user) throws
NoSuchRoomException {
try{
try{
rooms[roomID].quit(user);
room_Choice.select(getRoomName(roomID));
refreshList();
}catch(NotMemberException e){
}finally{
if ((rooms[roomID].getUserCount()==0)&&
(roomID!=DEFAULT_ROOM_ID)) {
room_Choice.remove(rooms[roomID].getRoomName());
rooms[roomID]=null;
}
}
}catch(ArrayIndexOutOfBoundsException e){
throw new NoSuchRoomException();
}catch(NullPointerException e) {
throw new NoSuchRoomException();
}
} // method 'quit' ended
public String[] getUsersName(int roomID) throws
NoSuchRoomException {
try{
if (rooms[roomID]!=null)
return rooms[roomID].getUsersName();
else return null;
}catch(ArrayIndexOutOfBoundsException e) {
throw new NoSuchRoomException();
}
} // method 'getUsersName' ended
public int getUserCount(int roomID) throws
NoSuchRoomException {
try{
return rooms[roomID].getUserCount();
}catch(ArrayIndexOutOfBoundsException e) {
throw new NoSuchRoomException();
}catch(NullPointerException e) {
throw new NoSuchRoomException();
}
} // method 'getUserCount' ended
public String getRoomName(int roomID) throws
NoSuchRoomException {
try{
return rooms[roomID].getRoomName();
}catch(ArrayIndexOutOfBoundsException e) {
throw new NoSuchRoomException();
}catch(NullPointerException e) {
throw new NoSuchRoomException();
}
} // method 'getRoomName' ended
public boolean isActive(int roomID) throws
NoSuchRoomException {
try{
if (rooms[roomID]!=null)
return true;
else return false;
}catch(ArrayIndexOutOfBoundsException e) {
throw new NoSuchRoomException();
}
}//method 'isActive' ended
public int roomCount() {
return rooms.length;
} //method 'roomCount' ended
} // class 'RoomGroup' ended
// class 3:'Room',RoomGroup引用的数据类
class Room {
String subject=null;
int ID=0;
Vector users=null;
Room(String s,int ID) {
subject=s;
this.ID=ID;
users=new Vector(10,5);
}// constructor 1 ended
public void join(User user) {
users.addElement(user);
user.roomID=ID;
user.paused=false;
}// method 'join' ended
public void quit(User user) throws
NotMemberException {
if (users.contains(user)) {
users.removeElement(user);
}else
throw new NotMemberException();
}// method 'quit' ended
public String[] getUsersName() {
String[] usersName=null;
if (users.size()!=0) {
usersName=new String[users.size()];
for(int i=0;i< usersName.length;i++) {
usersName[i]=((User)(users.elementAt(i))).name;
}
return usersName;
}else {
return null;
}
}
// method 'getUsersName' ended
public String getRoomName() {
return subject;
}// method 'getRoomName' ended
public int getUserCount() {
return users.size();
}// method 'getUserCount' ended
} // class 'Room' ended
//class 4:'User',服务器引用的用户服务线程类
class User extends Thread {
String name=null;
int ID=0;
boolean paused=false;
boolean used=false;
boolean belongRoom=false;
boolean logged=false;
DataInputStream io_in=null;
DataOutputStream io_out=null;
int roomID=0;
ServerSocket ss=null;
Socket s=null;
User[] users=null;
RoomGroup roomGroup=null;
TextArea message_TextArea=null;
ChatServer chatServer=null;
User(ChatServer chatServer,int ID) {
this.message_TextArea=chatServer.message_TextArea;
this.ss=chatServer.ss;
this.users=chatServer.users;
this.roomGroup=chatServer.roomGroup;
this.ID=ID;
this.chatServer=chatServer;
}// CONSTRATOR 1 ended
public void run() {
used=false;
try{
while(true){
String message=null;
s=ss.accept();
message_TextArea.appendText("\nChannel "+ID+"socket opened");
io_in=new DataInputStream(s.getInputStream());
io_out=new DataOutputStream(s.getOutputStream());
used=true;
logged=false;
try{
while(used) {
message=io_in.readUTF();
message_TextArea.appendText("\nChannel"+ID+"("+
name+"):receive:"+message);
chatServer.num_Line++;
if (chatServer.num_Line >150) {
message_TextArea.setText("");
chatServer.num_Line=0;
}
if (message.equals("text")) sendText();
else if (message.equals("draw")) sendDraw();
else {
commandCenter(message,new DataBag("none",
"none",0,0,0,0,0));
if ((message.equals("new"))|(message.equals
("join"))) refreshToAll();
else if (message.equals("quit"))
refreshToAllNotMe();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -