java吧 关注:1,290,344贴子:12,820,666
  • 12回复贴,共1

JAVA写聊天程序的思路是怎么的,求指教。

只看楼主收藏回复

刚学面向对象,学的云里雾里的,老师还给了我们一个简单的聊天程序的代码,结果发现看不懂。大家帮忙给点思路咯。


1楼2011-10-19 18:04回复
    没人啊


    2楼2011-10-19 18:07
    回复
      2025-12-07 22:34:01
      广告
      不感兴趣
      开通SVIP免广告
      你还没到那一步...先把基础打好了再看


      3楼2011-10-19 22:37
      回复
        我的作业就是写这个东东


        4楼2011-10-19 22:42
        回复
          发来看看


          5楼2011-10-19 22:47
          回复
            kage com.yidu._6;import java.awt.*;
            import java.awt.event.*;
            import java.util.EventListener;
            import java.io.*;
            import javax.swing.JComboBox;
            import java.net.*;
            import java.awt.event.ItemListener;
            public class Client extends Frame implements ActionListener, ItemListener {
            // 窗体应该有发送按钮、文本框用来输入东西、List列表用来显示信息 Button connectbutton = new Button("连接");
            Button sendbutton = new Button("发送"); List list = new List();
            TextField content = new TextField(20);
            TextField servername = new TextField(20);
            Label label = new Label("输入服务器名字:");
            JComboBox jComboBox1 = new JComboBox(); Socket socket;
            String ren = ""; public Client() { sendbutton.addActionListener(this);
            connectbutton.addActionListener(this);
            jComboBox1.addItemListener(this);
            Panel p1 = new Panel();
            p1.add(label);
            p1.add(servername);
            p1.add(connectbutton); Panel p2 = new Panel();
            p2.add(jComboBox1);
            p2.add(content);
            p2.add(sendbutton); Panel p3 = new Panel();
            p3.setLayout(new BorderLayout()); p3.add("South", p2);
            p3.add(list); this.add("South", p1);
            this.add("Center", p3); this.setSize(400, 400);
            this.jComboBox1.addItem("所有人");
            setDefaultCloseOperation(3); } private void setDefaultCloseOperation(int i) {
            // TODO Auto-generated method stub
            } public void actionPerformed(ActionEvent e) {
            if (e.getSource() == connectbutton) {
            if (!servername.getText().equals("")) {
            try {
            String fuwuqi = servername.getText();
            socket = new Socket(fuwuqi, 9000);
            new CInputStream(socket.getInputStream()).start();
            PrintWriter pw = new PrintWriter(socket.getOutputStream(),
            true);
            pw.println("CLogin:");
            this.connectbutton.setEnabled(false);
            this.servername.setText("");
            } catch (IOException ex2) {
            ex2.printStackTrace();
            }
            } else { javax.swing.JOptionPane.showMessageDialog(null,"请输入要连接的服务器名称");
            } } else {
            try {
            PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
            String shuo = content.getText(); System.out.println("ren" + ren);
            if (ren.equals("所有人")) {
            pw.println("CAll:" + this.getTitle() + "-" + ren + "="
            + shuo); this.list.add("你对" + ren + "说" + shuo);
            System.out.println("CAll:" + this.getTitle() + "-" + ren
            + "=" + shuo);
            } else {
            pw.println("COne:" + this.getTitle() + "-" + ren + "="
            + shuo);
            this.list.add("你对" + ren + "说:" + shuo);
            System.out.println("Cone"
            + "__________________________________");
            }
            } catch (IOException ex) {
            ex.printStackTrace();
            }
            }
            } public void itemStateChanged(ItemEvent e) {
            ren = (String) jComboBox1.getSelectedItem();
            } public static void main(String[] args) { Client c = new Client();
            QQClient.f = c;
            c.show();
            


            6楼2011-10-19 23:14
            回复
              }
              }class QQClient {
              public static Client f;
              }
              package com.yidu._6;
              import java.io.*;
              import java.net.*;
              import java.util.*;
              public class Server {
              public Server(){
              }
              public static void main(String[] args) {
              Hashtable ht=new Hashtable();
              try{
              ServerSocket ss=new ServerSocket(9000);
              int num=0;
              while(true){
              Socket s=ss.accept();
              String username=""+num;
              num++;
              ht.put(username,s);
              new ServerInputThread(s.getInputStream(),username,ht).start();
              System.out.println(username+"已登陆"); }
              }catch(Exception e){System.out.println(e);} }
              }
              //服务器输入流线程
              class ServerInputThread extends Thread{
              InputStream in; String ren;
              public ServerInputThread(InputStream in,String tmpusername,Hashtable ht){
              this.in = in;
              this.ren = tmpusername;
              Ssend.send=new send(ht);
              } public void run(){
              BufferedReader bf=new BufferedReader(new InputStreamReader(in));
              try{
              while(true){
              String str=bf.readLine();
              System.out.println(str);
              String cxiaoxi=str.substring(0,str.indexOf(":"));
              System.out.println(cxiaoxi);
              if(cxiaoxi.equals("CLogin")){
              Ssend.send.sendOne(ren,"SOneLogin:");
              System.out.println("------------------");
              Ssend.send.sendAll("SAllLogin:"+ren);
              String zaixian=Ssend.send.Zaixian();
              Ssend.send.sendOne(ren,"ZaiXian:"+zaixian);
              System.out.println("++++++++++++"+zaixian);
              }
              if(cxiaoxi.equals("COne")){
              String s1=str.substring(str.indexOf(":")+1);
              String s2=s1.substring(0,s1.indexOf("="));
              String s3=s1.substring(s1.indexOf("=")+1);
              String s4=s2.substring(0,s2.indexOf("-"));
              String s5=s2.substring(s2.indexOf("-")+1);
              Ssend.send.sendOne(s5,"SOne:"+s4+">"+s3);
              }
              if(cxiaoxi.equals("CAll")){
              String s1=str.substring(str.indexOf(":")+1);
              String s2=s1.substring(0,s1.indexOf("="));
              String s3=s1.substring(s1.indexOf("=")+1);
              String s4=s2.substring(0,s2.indexOf("-"));
              String s5=s2.substring(s2.indexOf("-")+1);
              Ssend.send.sendAll("SAll:"+s4+">"+s3);
              }
              } }catch(Exception e){System.out.println(e);}
              }}
              class send{
              Hashtable ht;
              public send(Hashtable ht){
              this.ht=ht;
              


              7楼2011-10-19 23:14
              回复
                } public void sendOne(String username,String message){
                Socket s=(Socket)ht.get(username);
                if(s==null){
                System.out.println("socket为空");
                }
                else
                {
                try{
                PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
                pw.println(username+"="+message);
                System.out.println(username+"="+message);
                }catch(Exception e){System.out.println(e);} } }
                public void sendAll(String message){
                Enumeration em=ht.keys();
                try{
                while(em.hasMoreElements()){
                String name=(String)em.nextElement();
                //System.out.println(name+"name");
                Socket socket=(Socket)ht.get(name);
                PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
                pw.println("="+message); }
                }catch(Exception e){System.out.println(e);}
                }
                public String Zaixian(){
                Enumeration em=ht.keys(); String zaixian="";
                while(em.hasMoreElements()){
                String name=(String)em.nextElement();
                zaixian=zaixian+name+";";
                }
                return zaixian;
                }
                }
                class Ssend{
                public static send send;
                }
                package com.yidu._6;import java.io.*;
                import java.net.*;
                class CInputStream extends Thread {
                InputStream in; public CInputStream(InputStream in) {
                this.in = in;
                } public void run() {
                BufferedReader buff = new BufferedReader(new InputStreamReader(in));
                while (true) {
                String s = null;
                try {
                s = buff.readLine();
                System.out.println(s);
                } catch (IOException ex) {
                }
                String sd = s.substring(s.indexOf("=") + 1);
                System.out.println(sd + "aaaaaaaaaaaaaaaaa");
                String ss = sd.substring(0, sd.indexOf(":"));
                System.out.println(ss + "bbbbbbbbbbbbb");
                if (ss.equals("SOneLogin")) {
                String sss = s.substring(0, s.indexOf("="));
                System.out.println(sss + "=============");
                QQClient.f.setTitle(sss);
                System.out.println(sss);
                QQClient.f.list.add(sss + "登录成功");
                }
                if (ss.equals("SAllLogin")) {
                String zaixian = sd.substring(sd.indexOf(":") + 1);
                System.out.println(zaixian + "-------------");
                String title = QQClient.f.getTitle();
                System.out.println("title" + title);
                if (!zaixian.equals(title)) {
                QQClient.f.jComboBox1.addItem(zaixian);
                QQClient.f.list.add(zaixian + "已登录");
                }
                }
                if (ss.equals("ZaiXian")) {
                String xian = sd.substring(sd.indexOf(":") + 1);
                String tmp[] = xian.split(";");
                for (int i = 0; i < tmp.length; i++) {
                if (!tmp[i].equals(QQClient.f.getTitle())) {
                QQClient.f.jComboBox1.addItem(tmp[i]);
                QQClient.f.list.add(tmp[i] + "已登录");
                }
                }
                } if (ss.equals("SOne")) {
                String xiaoxi = s.substring(s.indexOf(":") + 1);
                System.out.println("Sone" + xiaoxi);
                String s0 = xiaoxi.substring(0, xiaoxi.indexOf(">"));
                String str = s.substring(s.indexOf(">") + 1);
                QQClient.f.list.add(s0 + "对您说" + ":" + str);
                }
                if (ss.equals("SAll")) {
                String xiaoxi = s.substring(s.indexOf(":") + 1);
                String str = s.substring(s.indexOf(">") + 1);
                String s1 = xiaoxi.substring(0, xiaoxi.indexOf(">"));
                QQClient.f.list.add(s1 + "对所有人说" + "====" + str); }
                }
                }}
                看不很懂,老师要我们自己弄这个。


                8楼2011-10-19 23:14
                回复
                  2025-12-07 22:28:01
                  广告
                  不感兴趣
                  开通SVIP免广告
                  ...你确定你只教到面向对象?!那些用到的类教你了吗?多线程教了吗?还是你们面向对象是在最后教的的?


                  9楼2011-10-19 23:26
                  回复
                    都学了点基础知识,就讲了两张PPT==


                    10楼2011-10-19 23:43
                    回复
                      LZ老师牛。。就讲两张PPT,给这程序


                      IP属地:辽宁12楼2011-10-20 09:07
                      回复
                        一直在研究,就没看懂过


                        13楼2011-10-20 09:08
                        回复