?? serverframe.java
字號:
//package ZHWJBChat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
class ServerFrame extends JFrame
{
JPanel contentPane; // 定義圖形界面變量
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
static JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel();
static java.awt.List jList1 = new java.awt.List(13);
JScrollPane scrollpane = new JScrollPane(jList1);
static Vector clients = new Vector(10); // 用vector向量數組存儲連接客戶變量
static ServerSocket server = null; // 建立服務器socket
static int active_connects = 0; // 用來存儲目前連接的客戶數
static Socket socket = null;
public static void main(String[] args)
{
ServerFrame ServerFrame1=new ServerFrame(); // 實例化一個ServerFrame類
ServerFrame1.show();
System.out.println("Server starting ...");
try
{
server=new ServerSocket(8080); // 使用端口8080初始化服務器套接字
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
while(true)
{
try
{
socket = server.accept(); // 用來存儲連接上的客戶socket
if(socket != null)
{
System.out.println(socket + "連接"); // 在服務器控制臺打印客戶連接信息
}
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
ClientEchoThread c = new ClientEchoThread(socket); // 定義并實例化一個ClientEchoThread線程類,對應一個客戶連接
clients.addElement(c); // 加入clients數組中
if(checkName(c))
{ // 調用checkName方法驗證客戶的合法性
int connum = ++ServerFrame1.active_connects; // 定義connum來存儲活動連接數
String constr = "目前有" + connum + "客戶相連"; // 在狀態欄里顯示連接數
ServerFrame1.statusBar.setText(constr);
ServerFrame1.jList1.addItem(c.IP + "連接"); // 將客戶socket信息寫入list框
c.start();
String s = "SYSTEM:" + c.name + "加入聊天室!";
sendClients(s); // 啟動線程
notifyRoom(); // 用notifyRoom方法來監視聊天室連接變化
}
else
{
c.send("QUIT");
disconnect(c);
}
}
}
public ServerFrame()
{
try
{
jbInit();
}catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("聊天服務器端");
statusBar.setText("目前的連接數為:");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuHelpAbout_actionPerformed(e);
}
});
jPanel1.setLayout(borderLayout2);
jLabel1.setText("服務器端連接客戶");
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(scrollpane, BorderLayout.CENTER);
} // end of jbinit
public void jMenuFileExit_actionPerformed(ActionEvent e)
{ // 退出菜單方法
sendClients("QUIT"); // 向客戶端發送斷開連接信息
closeAll(); // 調用closeAll方法關閉所有連接
System.exit(0);
}
public void jMenuHelpAbout_actionPerformed(ActionEvent e)
{
chatServer_AboutBox dlg = new chatServer_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
}
protected void processWindowEvent(WindowEvent e)
{ // 關閉服務器程序要進行的操作
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jMenuFileExit_actionPerformed(null);
}
}
public static void notifyRoom()
{
String people = "PEOPLE";
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
people += ":" + c.name + '|' + c.IP;
}
sendClients(people);
}
public static synchronized void sendClients(String msg)
{ // 向每個連接的客戶端發送信息
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
c.send(msg);
}
}
public static void closeAll()
{ // 關閉所有連接信息
while(clients.size() > 0)
{ // 遍歷clients數組刪除所有連接客戶信息
ClientEchoThread c = (ClientEchoThread)clients.firstElement();
try
{
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
finally
{
clients.removeElement(c);
}
}
} //closeAll方法結束
public static boolean checkName(ClientEchoThread newclient)
{ // 檢查連接客戶的socket信息是否合法
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if((c != newclient) && c.equals(newclient.name)) return false;
}
return true;
} // end of checkName method
public static synchronized void disconnect(ClientEchoThread c)
{ // 斷開單個客戶方法
try
{
jList1.addItem(c.IP + "斷開連接"); // 在服務器端程序的list框中顯示斷開信息
int connum = --ServerFrame.active_connects; // 連接數減1
String constr = "目前有" + connum + "客戶相連"; // 刷新狀態欄里的連接數
statusBar.setText(constr);
c.send("QUIT"); // 向客戶發送斷開連接信息
//sendClients("SYSTEM:" + c.name + "離開聊天室!");
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
finally
{
clients.removeElement(c); // 從clients數組中刪除客戶的相關socket信息
}
}
static ClientEchoThread findFromName(String name)
{
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if(name.equals(c.name)) return c;
}
return null;
}
static String findIPFromName(String name)
{
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if(name.equals(c.name)) return c.IP;
}
return null;
}
}
class ClientEchoThread extends Thread
{ // 定義ClientEchoThread線程類
Socket socket; // 存儲一個連接客戶的socket信息
String name; // 存儲客戶的連接姓名
String IP;
String netMeetingFlag = "refuse"; // 存儲客戶的IP信息
BufferedReader dis; // 接收從客戶端發來的數據流
PrintWriter ps; // 向客戶端發送信息的打印流
public void send(String msg)
{ // 向客戶端發送信息的方法
ps.println(msg); // 打印流發送信息
ps.flush();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -