新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论Java, J2SE, J2ME, J2EE, 以及Eclipse, NetBeans, JBuilder等Java开发环境,还有JSP, JavaServlet, JavaBean, EJB以及struts, hibernate, spring, webwork2, Java 3D, JOGL等相关技术。
    [返回] 计算机科学论坛计算机技术与应用『 Java/Eclipse 』 → 这两个JAVA程序合起来是一个聊天室服务器,第一个有点问题,请教大家谁知道怎么合起来?还有第一个程序里面哪里错误   谢谢了 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2149 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 这两个JAVA程序合起来是一个聊天室服务器,第一个有点问题,请教大家谁知道怎么合起来?还有第一个程序里面哪里错误   谢谢了 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     woaisongru 帅哥哟,离线,有人找我吗?
      
      
      等级:大一新生
      文章:0
      积分:52
      门派:XML.ORG.CN
      注册:2006/4/26

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给woaisongru发送一个短消息 把woaisongru加入好友 查看woaisongru的个人资料 搜索woaisongru在『 Java/Eclipse 』的所有贴子 引用回复这个贴子 回复这个贴子 查看woaisongru的博客楼主
    发贴心情 这两个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();
        }
      }
    }


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/4/26 17:16:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Java/Eclipse 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/17 10:14:28

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    109.375ms