try {
while (true) {
Socket s = ss.accept();//While one client connected the server return one socket. client.add(s); //add the client to totalclient. client.elementCount++; client.currentclient++;
ServeoneJabber soj = new ServeoneJabber(s, client,user,num++); //Start the serveronejabber to serve the client.
System.out.println(\ } }
catch (IOException ioe) { ioe.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); } }
通过初始化,先生成ServerSocket: ss = new ServerSocket(PORT);
当启动多线程服务器后,服务器就处于监听状态。 Socket s = ss.accept();
当一个客户端进行连接后,服务器端便返回一个Socket,我们就可以利用这个Socket去找到任一对应的客户端,进行后面的发送消息等操作。 /**
* Send system message to the total client. * @param mess String * @throws IOException
* @return String the status of the result. * @author kentty * @version 1.0 */
public String castMessage(String mess) throws IOException {
boolean isSend=false; int i;
for (i = 0; i < client.elementCount; i++) {
Socket sss = (Socket)client.elementAt(i); System.out.println(sss);
PrintWriter everyout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(sss.getOutputStream())),true);
everyout.println(\系统广播: \ System.out.println(\
}
if (i == client.elementCount+1) {
isSend = true; }
if (isSend==true) {
return (mess); }
return \ }
}
//========================程序结束===========================//
4.3.2 服务器主界面
下面是服务器主界面程序分析: 设计思想:
StartServer类继承自Jframe类,提供操作界面,并实现ActionListener接口,进行事件侦听。
(1)界面设计,提供“启动服务器”、“关闭服务器”、“启动客户端”、“关闭客户端”、“发送系统广播”按扭,显示服务器状态信息的状态栏,提供“系统消息”文本框。
(2)当点击“启动服务器”后,启动一个MultiJabberServer对象,并进行端口侦听。 (3)发送系统广播,向每一个客户端发送消息。 (4)在信息提示栏显示相应的提示信息。
//============================程序开始===========================// //DBC StartServer.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*; import java.io.*;
public class StartServer extends JFrame implements ActionListener {
JFrame menuj; JFrame totaluserj; JFrame contentj;
JButton startserver; JButton stopserver; JButton startclient; JButton stopclient;
JTextField state;
JTextArea messagearea;
JScrollPane textAreaScrollPane;
// JTextArea totaluser;
JLabel tishi;
JTextField content; JButton send;
MultiJabberServer mjs; public StartServer() {
menuj = new JFrame(); totaluserj = new JFrame(); contentj = new JFrame();
startserver = new JButton(\启动服务器\ stopserver = new JButton(\关闭服务器\ startclient = new JButton(\启动客户端\ stopclient = new JButton(\关闭客户端\ state = new JTextField(15); state.setEditable(false);
// totaluser = new JTextArea();
tishi = new JLabel(\系统广播:\ content = new JTextField(20); send = new JButton(\发送\
messagearea = new JTextArea(); messagearea.setEditable(false);
textAreaScrollPane = new JScrollPane(messagearea);
menuj.getContentPane().setLayout(new FlowLayout()); menuj.getContentPane().add(startserver); menuj.getContentPane().add(stopserver); menuj.getContentPane().add(startclient); menuj.getContentPane().add(stopclient); menuj.getContentPane().add(state);
totaluserj.getContentPane().setLayout(new BorderLayout());
totaluserj.getContentPane().add(textAreaScrollPane,BorderLayout.CENTER);
contentj.getContentPane().setLayout(new BorderLayout()); contentj.getContentPane().add(tishi,BorderLayout.WEST);
contentj.getContentPane().add(content,BorderLayout.CENTER);
contentj.getContentPane().add(send,BorderLayout.EAST);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(menuj.getContentPane(),BorderLayout.NORTH); this.getContentPane().add(totaluserj.getContentPane(),BorderLayout.CENTER); this.getContentPane().add(contentj.getContentPane(),BorderLayout.SOUTH);
startserver.addActionListener(this); stopserver.addActionListener(this); startclient.addActionListener(this); stopclient.addActionListener(this); content.addActionListener(this); send.addActionListener(this); }
public static void main(String[] args) {
StartServer ss = new StartServer(); try {
ss.mjs = new MultiJabberServer(); }catch(IOException ioe) {
ss.showState(\ }
ss.setSize(600,400); ss.setVisible(true); ss.setResizable(false);
ss.setDefaultCloseOperation(EXIT_ON_CLOSE);
ss.setTitle(\ 欢迎使用JAVA爱好者BBS系统『服务器』\ }//
public void showState(String states) {
if (states.equals(\ {
state.setText(\服务器已启动...\ }
else if (states.equals(\ {
state.setText(\服务器已关闭!\ }
else if(states.equals(\ {
state.setText(\不能启动服务线程!\
} else
state.setText(states); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==content||e.getSource()==send) {
String mess= content.getText(); content.setText(\ try {
String messstr=this.mjs.castMessage(mess);
messagearea.append(\系统消息已发送: \ }catch(IOException ioe) {
showState(\不能发送消息!\ } }
else if (e.getSource()==startserver) {
this.mjs.start(); //启动服务器 showState(\ }
else if(e.getSource()==stopserver) {
this.mjs.stop(); showState(\ }
else if(e.getSource()==startclient) {
String [] args=new String[5]; new LoginFrame().main(args); }
else if(e.getSource()==stopclient) { } } }
//============================程序结束=========================//
4.3.3 登陆界面
设计思想: