| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1248 人关注过本帖
标题:如何编写网络聊天器,急啊
只看楼主 加入收藏
JacksonLi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-12-20
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:7 
如何编写网络聊天器,急啊
    系统功能:
    包括客户端和服务器,可以供多个用户进行聊天。
    1 登录功能。客户端登录到聊天服务器,服务器管理所有登录的客户,并将客户列表发送给各个客户显示。
    2 客户可以通过服务器转发,实现一对一和多对多聊天。
    3 实现呼叫功能。当客户端程序连接服务器时,通过服务器搜索所要呼叫的客户,如果检测到此用户且该用户正处于联网状态,则服务器通知此用户的客户端程序响应主叫方客户端程序,然后在主叫方和被叫方建立连接后,双方就可以聊天或进行其它的通信。  
    4 客户端程序应该可以实时显示目前其它用户的状态(例如好友信息,上、下线)。
    要求至少实现以上的大部分功能。
搜索更多相关主题的帖子: 聊天 编写 网络 
2009-12-21 12:43
吖…图图↗
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2009-12-21
收藏
得分:0 
我也想问这个,不过不要这么多要求,实现前2条就哦了
2009-12-21 17:28
xwlking
Rank: 4
来 自:湖南衡阳
等 级:业余侠客
威 望:1
帖 子:97
专家分:279
注 册:2009-11-1
收藏
得分:10 
前段时间在做一个聊天软件,功能没有你要求的那么强大,时间比较紧,所以只实现基本的一些功能。
其中服务器端的一些功能还没有实现,客户端的成员列表还没有弄好。仅供参考!   
服务器端代码如下:(可运行代码)服务器端可以字定义端口号,可向客户端发送系统消息。
程序代码:
import *;
import *;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.Vector;
public class Server extends JFrame implements ActionListener
{
    Container con;
    JLabel label1,label2,label3;
    JTextField field1,field2;
    static JTextArea area1=new JTextArea(7,30);
    JTextArea area2;
    JButton button1,button2,button3,button4;
    JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8,panel9,panelq,panelw;
    
    int k,i;
    String a,b;
    static String g;
    static Vector allsockets1;
    public static void main(String[] args)
    {
        new Server();
    }
    public Server(String area)
    {
        this.g=area;
        area1.append(g+"\n");
    }
    public Server(Vector v)
    {
        this.allsockets1=v;
    }
    public Server()
    {
        super("服务器端");
        label1=new JLabel("服务器IP:",JLabel.CENTER);
        label2=new JLabel("端口",JLabel.CENTER);
        label3=new JLabel(new ImageIcon("Love Message.jpg"));
        field1=new JTextField(13);
        field2=new JTextField("8000",13);
        button1=new JButton("开启");
        button2=new JButton("断开");
        button3=new JButton("发送");
        button4=new JButton("退出");
        panel1=new JPanel();
        panel2=new JPanel();
        panel3=new JPanel();
        panel4=new JPanel();
        panel5=new JPanel();
        panel6=new JPanel();
        panel7=new JPanel();
        panel8=new JPanel();
        panel9=new JPanel();
        panelq=new JPanel();
        panelw=new JPanel();
        //area1=new JTextArea(7,30);
        //area1.append(g);
        area2=new JTextArea(3,30);
        area1.setEditable(false);
        area1.setLineWrap(true);
        area2.setLineWrap(true);
        con=this.getContentPane();
        con.setLayout(new BorderLayout());
        con.add(panel1,BorderLayout.WEST);//西
        con.add(panel2,BorderLayout.CENTER);
        con.add(panel3,BorderLayout.EAST);
        panel2.setLayout(new BorderLayout());
        panel2.add(panel4,BorderLayout.NORTH);//北
        panel2.add(panel5,BorderLayout.CENTER);//中
        panel2.add(panel6,BorderLayout.EAST);//东
        panel2.add(panel7,BorderLayout.SOUTH);//南
        //panel5.setLayout(new BorderLayout());
        panel8.setLayout(new GridLayout(4,1,0,10));
        panel5.add(panel8);
        panel5.add(panel9);
        panel8.add(label1);
        panel8.add(field1);
        field1.setEditable(false);
        panel8.add(label2);
        panel8.add(field2);
        panel9.add(button1);
        panel9.add(button2);
        panel6.add(label3);
        panel7.setLayout(new BorderLayout());
        panel7.add(panelq,BorderLayout.NORTH);
        panel7.add(panelw,BorderLayout.CENTER);
        panelq.setLayout(new BorderLayout(0,5));
        panelq.add(new JScrollPane(area1),BorderLayout.NORTH);
        panelq.add(new JScrollPane(area2),BorderLayout.CENTER);
        panelw.add(button3);
        panelw.add(button4);
        button1.addActionListener(this);
        //button2.addActionListener(this);
        button3.addActionListener(this);
            
        try{
            InetAddress localHost=InetAddress.getLocalHost();
            System.out.println (localHost);
            String s=localHost.toString();
            field1.setText(s);
        }catch(Exception s){
            s.printStackTrace();
        }
        
        //this.setResizable(false);
        this.setLocation(200,160);
        this.setSize(365,500);
        this.setVisible(true);
    }
    public void actionPerformed(ActionEvent e)
    {
        a=this.field2.getText();
        i=Integer.parseInt(a);
        b=this.area2.getText();
        if(e.getSource()==button1)
        {
            new Sockets(i).start();
        }
        if(e.getSource()==button3)
        {
            //Vector allsockets1;
            if(b.equals(""))
            {
                JOptionPane.showMessageDialog(this,"管理员也不能发送空消息!");
                area2.requestFocus(true);
            }
            else
            {
                try{
                    for(int i=0;i<allsockets1.size();i++)
                    {
                        Object obj=allsockets1.get(i);//返回向量中指定位置的元素
                        Socket socket=(Socket)obj;
                        PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
                        pw.println("管理员:"+b);
                    }
                        area1.append("管理员: "+b+"\n");
                        area2.setText("");
                        area2.requestFocus(true);
                }catch(IOException o)
                {
                    System.out.println (o);
                }
            }
        }
    }
}
class Sockets extends Thread
{
    int i;
    public Sockets(int i)
    {
        this.i=i;
    }
    public void run()
    {
        try{
            System.out.println (i);
            System.out.println ("我是服务器");
            ServerSocket ss=new ServerSocket(i);//建立一个Socket,断口为8000
            Vector v=new Vector();
            while(true)
            {
                Socket s=ss.accept();//等待客户端连接(阻塞)
                InputStream in=s.getInputStream();//得到输入流
                v.add(s);
                new ServerReceiveThread(in,v).start();
                new Server(v);
            }
        }catch(IOException i){
            i.printStackTrace();
        }
    }
}
class ServerReceiveThread extends Thread
{
    InputStream in;
    Vector allsockets;
    Vector r=new Vector();
    int k;
    String h;
    public ServerReceiveThread(InputStream in,Vector v)
    {
        this.in=in;
        this.allsockets=v;
    }
    public void run()
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(in));
        try{
            while(true)
            {
                String str=br.readLine();
                System.out.println (str);
                if(str.equals(""))
                {
                    k++;
                    h="游客"+k+"号连接";
                    new Server(h);
                    //r.add(h);
                }
                else
                {
                    h=str;
                    new Server(h);
                    //r.add(h1);
                }
                for(int i=0;i<allsockets.size();i++)
                {
                    Object obj=allsockets.get(i);//返回向量中指定位置的元素
                    Socket socket=(Socket)obj;
                    PrintWriter pw=new PrintWriter(socket.getOutputStream(),true);
                    pw.println(h);
                    /*if(str.equals(""))
                    {
                        k++;
                        pw.println("游客"+k+"号连接");
                        //h="游客"+k+"号连接";
                        //new Server(h);
                        //r.add(h);
                    }
                    else
                    {
                        pw.println(str);
                        //h1=str;
                        //new Server(h);
                        //r.add(h1);
                    }*/
                    //r.add(h);
                    //pw.println(r);
                }
                System.out.println ("该消息已发送");
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
/*class Shuchu extends Thread
{
    OutputStream out;
    public Shuchu(OutputStream out)
    {
        this.out=out;
    }
    public void run()
    {
        PrintWriter pw=new PrintWriter(out,true);
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        while(true)
        {
            try{
                pw.println(br.readLine());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}*/

客户端代码如下:(可运行代码)客户端昵称可填可不填,如果在本机上测试IP为127.0.0.1  端口必须和服务器设置的一样。
程序代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import *;
import *;
import java.awt.event.*;
import java.util.Date;
public class Client extends JFrame implements ActionListener
{
    JButton button1,button2,button3;
    JLabel label1,label2,label3,label4,label5;
    JTextArea area1,area2,area3;
    JTextField field1,field2,field3;
    JPanel panel1,panel2,panel3,panel4,panel5,panel6,panel7;
    Container con;
    String a,b,g,q;
    int c;
    Socket socket;
    public Client(String s)
    {
        String x=s;
        JOptionPane.showMessageDialog(this,x);
    }
    public Client()
    {        
        super("客户端");
        panel1=new JPanel();
        panel2=new JPanel();
        panel3=new JPanel();
        panel4=new JPanel();
        panel5=new JPanel();
        panel6=new JPanel();
        panel7=new JPanel();

        con=this.getContentPane();
        con.setLayout(new BorderLayout());
        con.add(panel1,BorderLayout.WEST);
        con.add(panel2,BorderLayout.CENTER);
        con.add(panel3,BorderLayout.EAST);
        panel2.setLayout(new BorderLayout(5,0));
        panel2.add(panel4,BorderLayout.NORTH);
        panel2.add(panel5,BorderLayout.WEST);
        panel2.add(panel6,BorderLayout.CENTER);
        panel2.add(panel7,BorderLayout.SOUTH);
        panel5.setLayout(new BorderLayout(0,10));
        
        
        area1=new JTextArea(10,30);
        area2=new JTextArea(4,30);
        area1.setLineWrap(true);
        area2.setLineWrap(true);
        panel5.add(new JScrollPane(area1),BorderLayout.CENTER);
        area1.setEditable(false);
        panel5.add(new JScrollPane(area2),BorderLayout.SOUTH);
        
        button1=new JButton("发送");
        button2=new JButton("取消");
        button3=new JButton("连接");
        
        label1=new JLabel("成员列表:");
        label2=new JLabel("我的昵称:");
        label3=new JLabel("IP地址:");
        label4=new JLabel("端口:");
        label5=new JLabel();
        field1=new JTextField(10);
        field2=new JTextField(10);
        field3=new JTextField(10);
        area3=new JTextArea(8,10);
        //panel6.setLayout(new GridLayout(9,1,5,5));
        panel6.add(label1);
        panel6.add(new JScrollPane(area3));
        panel6.add(label2);
        panel6.add(field1);
        panel6.add(label3);
        panel6.add(field2);
        panel6.add(label4);
        panel6.add(field3);
        panel6.add(button3);
        panel6.add(label5);
        
        panel7.add(button1);
        panel7.add(button2);
        
        button1.addActionListener(this);
        button2.addActionListener(this);
        button3.addActionListener(this);
        new Shijian(label5).start();
        
        try{
            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            this.repaint();
           }
           catch(Exception ext){
               ext.printStackTrace();
           }
        
        //this.setIconImage(Toolkit.getDefaultToolkit().createImage("1.jpg"));
        
        this.setSize(500,460);
        this.setLocation(260,160);
        this.setResizable(false);
        this.setVisible(true);
        field2.requestFocus(true);
    }
    
    /*public void join(String s)
    {
        c=Integer.parseInt(b);
        try{
            System.out.println ("我是客户端");
            //连接断口为8000的服务器,localhost表示本机,可改为服务器的IP地址
            Socket sk=new Socket(s,c);
            InputStream in=sk.getInputStream();
            OutputStream out=sk.getOutputStream();//得到输出流
            new Shuru(in).start();
            new Shuchu(out).start();
            //PrintStream ps=new PrintStream(os,true);//输出字符流
            //ps.println("你好服务器");
            //InputStream is=sk.getInputStream();//得到输入流
            //InputStreamReader isr=new InputStreamReader(is);//输入字节流
            //BufferedReader br=new BufferedReader(isr);//输入字符流
            //System.out.println ("服务器说:"+br.readLine());//读行
        }catch(IOException e){
            e.printStackTrace();
        }
    }*/
    
    public void actionPerformed(ActionEvent e)
    {
        a=this.field2.getText();
        b=this.field3.getText();
        q=this.field1.getText();
        g=this.area2.getText();
        if(e.getSource()==button1)
        {
            if(g.equals(""))
            {
                JOptionPane.showMessageDialog(this,"不能发送空消息!");
                area2.requestFocus(true);
            }
            else
            {
                try{
                    OutputStream out=socket.getOutputStream();
                    PrintWriter pw=new PrintWriter(out,true);
                    pw.println(q+"    "+new Date().toLocaleString()+"\n"+"     "+g);
                    area2.setText("");
                    area2.requestFocus(true);
                }catch(Exception x){
                    x.printStackTrace();
                }
            }
        }
        if(e.getSource()==button3)
        {
            if(a.equals("")&&b.equals(""))
            {
                JOptionPane.showMessageDialog(this,"IP地址或端口为空!");
                field2.requestFocus(true);
            }
            else if(a.equals(""))
            {
                JOptionPane.showMessageDialog(this,"请填写IP地址");
                field2.requestFocus(true);
            }
            else if(b.equals(""))
            {
                JOptionPane.showMessageDialog(this,"请填写端口");
                field3.requestFocus(true);
            }
            else
            {
                //this.join(a);
                try{
                    socket=new Socket(a,Integer.parseInt(b));
                    field1.setEditable(false);
                    field2.setEditable(false);
                    field3.setEditable(false);
                    button3.setEnabled(false);
                    button3.setText("连接中...");
                    JOptionPane.showMessageDialog(this,"连接成功!");
                    area2.requestFocus(true);
                    new ClientReceiveThread(socket.getInputStream(),area1,area3).start();
                }catch(Exception x){
                    JOptionPane.showMessageDialog(this,"连接服务器失败!");
                    x.printStackTrace();
                }
                if(q.equals(""))
                {
                    try{
                        OutputStream out=socket.getOutputStream();
                        PrintWriter pw=new PrintWriter(out,true);
                        pw.println("");
                        BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                            String str=br.readLine();
                            area1.append(str+"\n");
                            field1.setText(str.replace("连接",""));//String类的方法(替换)
                            area3.append(str.replace("连接","")+"\n");
                    }catch(Exception x){
                        x.printStackTrace();
                    }
                }
                else
                {
                    try{
                        OutputStream out=socket.getOutputStream();
                        PrintWriter pw=new PrintWriter(out,true);
                        pw.println(q+"连接");
                    }catch(Exception x){
                        x.printStackTrace();
                    }
                }
            }
        }
    }
    
    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        new Client();
    }
}
class ClientReceiveThread extends Thread
{
    InputStream in;
    JTextArea area4,area5;
    public ClientReceiveThread(InputStream in,JTextArea area,JTextArea area1)
    {
        this.in=in;
        this.area4=area;
        this.area5=area1;
    }
    public void run()
    {
        try{
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            while(true)
            {
                String str=br.readLine();
                area4.append(str+"\n");
                if(str.endsWith("连接"))//测试此字符串是否以指定的后缀结束。
                {
                    area5.append(str.replace("连接","")+"\n");
                }
            }
        }catch(Exception e)
        {
            String s="服务器已断开连接!";
            new Client(s);
            e.printStackTrace();
        }
    }
}
class Shijian extends Thread
{
    JLabel label;
    public Shijian(JLabel label)
    {
        this.label=label;
    }
    public void run()
    {
        while(true)
        {
            try{
                label.setText(new Date().toLocaleString());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}
/*class Shuchu extends Thread
{
    OutputStream out;
    public Shuchu(OutputStream out)
    {
        this.out=out;
    }
    public void run()
    {
        PrintWriter pw=new PrintWriter(out,true);
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        while(true)
        {
            try{
                pw.println(new Client().g);//br.readLine());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}*/

人总需相信自己,人总需依靠自己. ---永远的Beyond---
2009-12-21 19:03
超级修补匠
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-12-23
收藏
得分:0 
一定要好好拜读!
2009-12-24 17:10
JacksonLi
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2009-12-20
收藏
得分:0 
多谢
2009-12-24 19:20
xiaoxiaoyun
Rank: 2
等 级:论坛游民
帖 子:8
专家分:15
注 册:2009-11-10
收藏
得分:0 
我也想要个,谢谢了.
2009-12-25 16:57
pywepe
Rank: 6Rank: 6
等 级:侠之大者
威 望:4
帖 子:296
专家分:483
注 册:2009-4-5
收藏
得分:0 
以下是引用JacksonLi在2009-12-21 12:43:20的发言:

    系统功能:
    包括客户端和服务器,可以供多个用户进行聊天。
    1 登录功能。客户端登录到聊天服务器,服务器管理所有登录的客户,并将客户列表发送给各个客户显示。
    2 客户可以通过服务器转发,实现一 ...

我写过

类似的

不过写得不好 也没去管它了

java群
62635216
欢迎加入
2009-12-25 17:49
h1098879464
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2019-6-10
收藏
得分:0 
好复杂啊
2019-06-10 17:36
快速回复:如何编写网络聊天器,急啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038664 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved