以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 Java/Eclipse 』  (http://bbs.xml.org.cn/list.asp?boardid=41)
----  这两个JAVA程序合起来是一个聊天室服务器,第一个有点问题,请教大家谁知道怎么合起来?还有第一个程序里面哪里错误   谢谢了  (http://bbs.xml.org.cn/dispbbs.asp?boardid=41&rootid=&id=31326)


--  作者:woaisongru
--  发布时间:4/26/2006 5:16:00 PM

--  这两个JAVA程序合起来是一个聊天室服务器,第一个有点问题,请教大家谁知道怎么合起来?还有第一个程序里面哪里错误   谢谢了
第一个
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class chatServer extends JFrame {
  JPanel contentPane;
  JMenuBar jMenuBar1 = new JMenuBar();
  JMenu jMenuFile = new JMenu();
  JMenuItem jMenuFileExit = new JMenuItem();
  JMenu jMenuHelp = new JMenu();
  JMenuItem jMenuHelpAbout = new JMenuItem();
  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);
  static ServerSocket server=null;
  static int active_connects=0;
  static Socket socket=null;
  public static void main(String[] args) {
   try {
   UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
   }
   catch(Exception e) {
    e.printStackTrace();
   }
   chatServer chatServer1=new chatServer();
   chatServer1.show();
   System.out.println("正在启动服务器...");
   try
      {
      server=new ServerSocket(2525);
   }
   catch(IOException e)
      {
      System.out.println("Error:"+e);
   }
   while(true)
      {
      if(clients.size()<5)
         {
         try
            {
            socket=server.accept();
            if(socket!=null)
            {
            System.out.println(socket+"连接");
         }
      }
      catch(IOException e)
         {
         System.out.println("Error:"+e);
      }
      int i=0;
         do{
         Client c=new Client(socket);
         clients.addElement(c);
         if(checkName(c))
           {
           int connum=++chatServer1.active_connects;
           String constr="目前有"+connum+"个客户相连";
           chatServer1.statusBar.setText(constr);
           Client listdata=(Client)clients.elementAt(i);
           chatServer1.jList1.addItem(listdata.ip+"连接",i);
           c.start();
           notifyRoom();
           }
           else
             {
             c.ps.println("TAKEN");
             disconnect(c);
             }
             i++;
             break;
         }
         while(i<clients.size());
      }
      else
         {
         try{Thread.sleep(200);}
         catch(InterruptedException e)
            {
         }
      }
   }
}
public chatServer()
{
  enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  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("文件");
  jMenuFileExit.setText("退出");
  jMenuFileExit.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    jMenuFileExit_actionPerformed(e);
   }
  };
  jMenuHelp.setText("帮助");
  jMenuHelpAbout.setText("关于");
  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);
}
public void jMenuFileExit_actionPerformed(ActionEvent e)
{
  sendClients(new StringBuffer("QUIT"));
  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()
  {
  StringBuffer people=new StringBuffer("PEOPLE");
   for(int i=0;i<clients.size();i++)
      {
      Client c=(Client)clients.elementAt(i);
      people.append(":"+c.name);
   }
   sendClients(people);
}
public static synchronized void sendClients(StringBuffer msg)
   {
   for(int i=0;i<clients.size();i++)
      {
      Client c=(Client)clients.elementAt(i);
      c.send(msg);
   }
}
public static void closeAll()
   {
   while(clients.size()>0)
      {
      Client c=(Client)clients.firstElement();
      try
         {
         c.socket.close();
      }
      catch(IOException e)
         {
         System.out.println("Error:"+e);
      }
         finally
            {
            clients.removeElement(c);
         }
      }
   }
   public static boolean checkName(Client newlient)
      {
      for(int i=0;i<clients.size();i++)
         {
         Client c=(Client)clients.elementAt(i);
         if((C!=newclient)&&c.equals(newclient.name))
            return false;
      }
      return(true);
   }
   public static synchronized void disconnect(Client c)
      {
      try
         {
         jList1.addItem(c.ip+"断开连接");
         chatServer.active_connects--;
            c.send(new StringBuffer("退出"));
         c.socket.close();
      }
      catch(IOException e)
         {
         System.out.println("Error:"+e);
      }
      finally
         {
         clients.removeElement(c);
      }
}
class Client extends Thread
{
   Socket socket;
   String name;
   String ip;
   DataInputStream dis;
   PrintStream ps;
   public void send(StringBuffer msg)
      {
      ps.println(msg);
      ps.flush();
   }
   public Client(Socket s)
      {
      socket=s;
      try
         {
         dis=new DataInputStream(s.getInputStream());
         ps=new PrintStream(s.getOutputStream());
         String info=dis.readLine();
         StringTokenizer stinfo=new StringTokenizer(info,":");
         String head=stinfo.nextToken();
          if(stinfo.hasMoreTokens())
         name=stinfo.nextToken();
         if(stinfo.hasMoreTokens())
         ip=stinfo.nextToken();
         System.out.println(head);
      }
      catch(IOException e)
         {
         System.out.println("Error:"+e);
      }
   }
   public void run()
      {
      while(true)
         {
         String line=null;
         try
           {
           line=dis.readLine();
         }
         catch(IOException e)
         {
         System.out.println("Error"+e);
         chatServer.disconnect(this);
         chatServer.notifyRoom();
         return;
      }
      if(line==null)
         {
         chatServer.disconnect(this);
         chatServer.notifyRoom();
         return;
         }
      StringTokenizer st=new StringTokenizer(line,":");
      String keyword=st.nextToken();
      if(keyword.equals("MSG"))
         {
            StringBuffer msg=new StringBuffer("MSG:");
         msg.append(name);
         msg.append(st.nextToken("\0"));
         chatServer.sendClients(msg);
      }
      else if(keyword.equals("退出"))
         {
            chatServer.disconnect(this);
         chatServer.notifyRoom();
         this.stop();
      }
   }
}


第二个
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class chatServer_AboutBox extends JDialog implements
ActionListener {
  JPanel panel1 = new JPanel();
  JPanel panel2 = new JPanel();
  JPanel insetsPanel1 = new JPanel();
  JPanel insetsPanel2 = new JPanel();
  JPanel insetsPanel3 = new JPanel();
  JButton button1 = new JButton();
  JLabel imageLabel = new JLabel();
  JLabel label1 = new JLabel();
  JLabel label2 = new JLabel();
  JLabel label3 = new JLabel();
  JLabel label4 = new JLabel();
  BorderLayout borderLayout1 = new BorderLayout();
  BorderLayout borderLayout2 = new BorderLayout();
  FlowLayout flowLayout1 = new FlowLayout();
  GridLayout gridLayout1 = new GridLayout();
  String product = "简易聊天服务器端";
  String version = "1.0";
  String copyright = "Copyright (c) 2002";
  String comments = "本聊天室服务器端实现了多线程客户连接和显示连接信息";
  public chatServer_AboutBox(Frame parent) {
    super(parent);
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try{
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    pack();
  }
  private void jbInit() throws Exception {
    this.setTitle("关于");
    setResizable(false);
    panel1.setLayout(borderLayout1);
    panel2.setLayout(borderLayout2);
    insetsPanel1.setLayout(flowLayout1);
    insetsPanel2.setLayout(flowLayout1);
    insetsPanel2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
    gridLayout1.setRows(4);
    gridLayout1.setColumns(1);
    label1.setText(product);
    label2.setText(version);
    label3.setText(copyright);
    label4.setText(comments);
    insetsPanel3.setLayout(gridLayout1);
    insetsPanel3.setBorder(BorderFactory.createEmptyBorder(10,60,10,10));
    button1.setText("确定");
    button1.addActionListener(this);
    insetsPanel2.add(imageLabel, null);
    panel2.add(insetsPanel2, BorderLayout.WEST);
    this.getContentPane().add(panel1, null);
    insetsPanel3.add(label1, null);
    insetsPanel3.add(label2, null);
    insetsPanel3.add(label3, null);
    insetsPanel3.add(label4, null);
    panel2.add(insetsPanel3, BorderLayout.CENTER);
    insetsPanel1.add(button1, null);
    panel1.add(insetsPanel1, BorderLayout.SOUTH);
    panel1.add(panel2, BorderLayout.NORTH);
  }
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      cancel();
    }
    super.processWindowEvent(e);
  }
  void cancel() {
    dispose();
  }
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1) {
      cancel();
    }
  }
}


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
93.750ms