我们老师要我们做一个聊天室的项目,要求:
1用户注册和登录(要连数据库)
2用户在线列表
3用户公聊和私聊
4传送文件
小弟的水平十分有限,头都想大了就是搞不出,请问各位高手有没有现成的聊天室源程序文件或是其它的可参考的源程序文件来要我学习一下,不要太复杂,只要实现所要求的功能就好,我的qq是461127214,邮箱是[email protected],谢谢各位了

解决方案 »

  1.   

    那你应该再看看 Socket 
      

  2.   

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;public class ChatClient extends Frame{
        TextArea ta=new TextArea(10,40);//消息接收显示框
        TextField tf=new TextField(20);//消息编写框
        TextField ipInput=new TextField("192.168.0.189",10);//IP输入框
        TextField portInput=new TextField("4444",2);//输入端口
        TextField nameInput=new TextField("zion2008",5);//输入端口
        Button conn=new Button("登录");
        Button b=new Button("发送");//发送消息按钮
        Socket client=null;
        public ChatClient() {
            super("聊天室客户端");
            ta.setBackground(new Color(1,60,60));
            ta.setForeground(Color.white);
            ta.setEditable(false);
            this.add(ta,BorderLayout.NORTH);
            Panel p=new Panel();
            p.add(new Label("IP"));
            p.add(ipInput);
            p.add(new Label("Port"));
            p.add(portInput);
            p.add(new Label("Name"));
            p.add(nameInput);
            p.add(conn);
            p.add(tf);
            p.add(b);
            this.add(p,BorderLayout.CENTER);
            this.setResizable(false);//不能调节窗口大小
            MyActionListener lis=new MyActionListener();
            this.addWindowListener(lis);
            conn.addActionListener(lis);//给登录按钮添加事件处理器
            b.addActionListener(lis);
            this.pack();
            this.show();
        }
        public static void main(String[] args) {
            ChatClient cc = new ChatClient();
        }
        //内部类,做事件处理
        class MyActionListener extends WindowAdapter implements ActionListener{
            public void actionPerformed(ActionEvent e){
                String cmd=e.getActionCommand();
                if(cmd.equals("登录")){
                    String tmp=null;
                    try {
                        //创建到指定服务器的socket连接
                        client = new Socket(ipInput.getText(),Integer.parseInt(portInput.getText()));
                        tmp="[系统消息]:连接成功\n";
                        ReadMsg reader=new ReadMsg();
                        reader.start();
                    } catch (Exception ex) {
                        tmp="[系统消息]:连接失败\n";
                    }
                    ta.setText(tmp);//显示登录成功或失败信息
                }
                if(cmd.equals("发送")){
                    OutputStream os =null;
                    PrintStream ps =null;
                    try {
                        os = client.getOutputStream();
                        ps = new PrintStream(os);
                        ps.println(tf.getText());
                    } catch (Exception ex1) {
                        //ex1.printStackTrace();
                        ta.append("[系统消息]:发送失败\n");
                    }
                }
            }
            public void windowClosing(WindowEvent e){
                System.exit(0);//关闭窗口,退出程序
            }
        }    //内部类,负责接收从服务器上转发回的消息
        class ReadMsg extends Thread{
            public void run(){
                InputStream is =null;
                BufferedReader br=null;
                String msg = null;
                OutputStream os = null;
                PrintStream ps = null;
                try {
                    is = client.getInputStream();
                    br = new BufferedReader(new InputStreamReader(is));                //客户端连接成功后,首先发送昵称过去
                    os = client.getOutputStream();
                    ps = new PrintStream(os);
                    ps.println(nameInput.getText());
                } catch (Exception ex1) {
                    //ex1.printStackTrace();
                }            while(true){
                    try {
                        while ((msg = br.readLine()) != null) {
                            ta.append(msg + "\n");
                        }
                    } catch (Exception ex) {
                        //ex.printStackTrace();
                    }
                }
            }
        }
    }这个是聊天室的客户端
      

  3.   

    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.net.*;
    import java.util.Collection;
    import java.util.Hashtable;
    import java.util.Iterator;public class ChatServer extends Frame{
        TextArea ta=new TextArea(20,80);//消息接收显示框
        TextField tf=new TextField(60);//消息编写框
        Button b=new Button("  发  送  ");//发送消息按钮
        Button start=new Button("启动服务");
        ServerSocket server=null;
        TextField portInput=new TextField("4444",2);
        List userList=new List(21);
        Button refreshUser=new Button("  刷 新 用 户 列 表  ");
        //Vector users=new Vector();
        Hashtable hash=new Hashtable();
        public ChatServer() {
            super("聊天室服务器端");
            ta.setBackground(new Color(1,60,160));
            ta.setForeground(Color.white);
            ta.setEditable(false);
            //this.add(ta,BorderLayout.NORTH);
            Panel p=new Panel(new FlowLayout(FlowLayout.LEFT));
            p.add(new Label("Port"));
            p.add(portInput);
            p.add(start);
            p.add(tf);
            p.add(b);        Panel pleft=new Panel(new BorderLayout());
            pleft.add(ta,BorderLayout.NORTH);
            pleft.add(p,BorderLayout.CENTER);
            Panel pright=new Panel(new BorderLayout());
            userList.setBackground(new Color(1,60,60));
            userList.setForeground(Color.white);        Panel pb=new Panel();
            pb.add(refreshUser);        pright.add(pb,BorderLayout.CENTER);
            pright.add(userList,BorderLayout.NORTH);
            //this.add(p,BorderLayout.CENTER);
            this.add(pleft,BorderLayout.CENTER);
            this.add(pright,BorderLayout.EAST);
            this.setResizable(false);//不能调节窗口大小
            MyActionListener lis=new MyActionListener();
            this.addWindowListener(lis);
            start.addActionListener(lis);
            b.addActionListener(lis);
            refreshUser.addActionListener(lis);
            this.pack();
            this.show();
        }
        public static void main(String[] args) {
            ChatServer s = new ChatServer();
        }
        public void showUserList(){
           userList.clear();
           Collection col=hash.keySet();
           Iterator it=col.iterator();
           int i=0;
           while(it.hasNext()){
                String name=(String)it.next();
                userList.add(name);
                i++;
           }
           this.setTitle("聊天室服务器端-当前在线"+i+"位用户");
        }
        //广播消息
        public void broadCast(String msg) {        Collection col=hash.values();
            Iterator it=col.iterator();
            while(it.hasNext()){
                try {
                    Socket c = (Socket) it.next();
                    OutputStream os = c.getOutputStream();
                    PrintStream ps = new PrintStream(os);
                    ps.println(msg);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            }
        //内部类,做事件处理
        class MyActionListener extends WindowAdapter implements ActionListener{
            Service service = new Service();
            public void actionPerformed(ActionEvent e) {
                String cmd = e.getActionCommand();
                if(cmd.equals("启动服务")){
                    String tmp=null;
                    try {
                        if(server!=null&&!server.isClosed())
                            server.close();
                        server = new ServerSocket(Integer.parseInt(portInput.getText()));
                        service.start();
                        tmp="[系统消息]:服务器启动成功\n";
                    } catch (Exception ex) {
                        tmp="[系统消息]:服务器启动失败\n";
                        //ex.printStackTrace();
                    }
                    ta.setText(tmp);
                }
                if(cmd.equals("  发  送  ")){
                    broadCast("[版主]:"+tf.getText());
                    ta.append("[版主]:"+tf.getText()+"\n");
                }
                if(cmd.equals("  刷 新 用 户 列 表  "))
                    showUserList();
            }
            public void windowClosing(WindowEvent e){
                System.exit(0);//关闭窗口,退出程序
            }
        }
        class Service extends Thread {
            public void run() {
               while (true) {
                    try {
                        Socket client = server.accept();
                        ServiceUnit unit=new ServiceUnit(client);
                        unit.start();                    Thread.sleep(100);
                    } catch (Exception ex) {
                        //ex.printStackTrace();
                        break;
                    }
                    }
            }
        }
        class ServiceUnit extends Thread{
            Socket client = null;
            InputStream is = null;
            BufferedReader br = null;
            String msg = null;
            String returnMsg=null;
            int index=0;
            String name=null;
            public ServiceUnit(Socket c) {
                client=c;
            }
            public void run() {
                //users.add(client);
                while (true) {
                    try {
                        is = client.getInputStream();
                        br = new BufferedReader(new InputStreamReader(is));
                        msg = null;                    while ((msg = br.readLine()) != null) {
                            index++;
                            if (index == 1) {
                                name = msg+"-"+client.getInetAddress().getHostAddress();
                                ta.append("[" + name + "]进入聊天室\n");
                                returnMsg = "[版主]:欢迎" + name + "进入聊天室";
                                hash.put(name,client);
                                showUserList();
                            } else {
                                ta.append("[" + name + "]:" + msg + "\n");
                                returnMsg = "[" + name + "]:" + msg;
                            }
                            broadCast(returnMsg);
                        }
                        Thread.sleep(100);
                    } catch (Exception ex) {
                        //ex.printStackTrace();
                        break;
                    }
                }
                hash.remove(name);
                broadCast("[" + name + "]走了");
                ta.append("[" + name + "]走了\n");
                showUserList();
            }
        }
    }这个是服务端,必须启动服务端,客户端才可以访问到服务端,你自己试试看吧
    简单的聊天功能已经实现了,要实现其他的功能你自己加吧