码迷,mamicode.com
首页 > 编程语言 > 详细

基于UDP协议的聊天室(java实现)

时间:2017-07-11 15:53:11      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:about   使用   参数配置   vax   pen   ext   append   getx   extends   

主要思路很简单:

1.设置自己的接收端口

2.设置对方IP和端口

3.发送数据和接收数据

 

下面是主要代码:

技术分享
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class QQFrame extends JFrame implements Runnable
{
    private JTextArea sendArea = new JTextArea(20,20);
    private JTextArea showArea = new JTextArea(10,20);
    private JButton sendButton = new JButton("发送");
    private MenuBar menuBar = new MenuBar();
    private Menu setMenu = new Menu("设置");
    private Menu helpMenu = new Menu("帮助");
    private MenuItem aboutItem = new MenuItem("关于");    
    private MenuItem setItem = new MenuItem("配置参数");
    private Thread thread = new Thread(this);
    private Config config = new Config();
    private JDialog dialog = new JDialog(this,"参数配置",true);
    private int myPort;
    QQFrame(int in_myPort)
    {
        super("局域网聊天工具");
        setResizable(false);
        myPort = in_myPort;
        setItem.addActionListener(new SetDialog(dialog,this));
        aboutItem.addActionListener(new AboutDialog(this));
        initLayout();
        sendButton.addActionListener(new SendListener(this,sendArea,showArea,config));
        thread.start();
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
    void initLayout()
    {
        int w,h;
        w = 400;
        h = 500;
        setBounds(100,100,w,h);
        setVisible(true);
        setLayout(new GridLayout(3,1,10,15));
        JPanel jp1,jp2;
        jp1 = new JPanel();
        jp2 = new JPanel();
        JLabel jl1;
        jl1 = new JLabel("聊天记录框:");
        jp1.setLayout(null);
        jp1.add(jl1);
        jl1.setBounds(0,0,w,20);
        jp1.add(showArea);
        showArea.setBounds(0,30,w,h/3-20);
        JLabel jl2;
        jp2.setLayout(null);
        jl2 = new JLabel("发送框:");
        jp2.add(jl2);
        jl2.setBounds(0,0,w,20);
        sendArea.setBounds(0,30,w,h/3-20);
        jp2.add(sendArea);
        JPanel jp3 = new JPanel();
        add(jp1);
        add(jp2);
        add(jp3);
        jp3.add(sendButton);
        setMenuBar(menuBar);
        menuBar.add(setMenu);
        menuBar.add(helpMenu);
        helpMenu.add(aboutItem);
        setMenu.add(setItem);
        dialog.setBounds(120,150,200,250);
        JLabel IPLabel = new JLabel("对方IP");
        JLabel yourPortLabel = new JLabel("对方接收端口");
        JLabel nameLabel = new JLabel("本机名");
        JTextField IPTextField = new JTextField(15);
        JTextField yourPortTextField = new JTextField(8);
        JTextField nameTextField = new JTextField(15);
        IPTextField.setText("127.0.0.1");
        yourPortTextField.setText("879");
        nameTextField.setText("hanxi");
        
        dialog.setLayout(new GridLayout(4,2,10,10));
        dialog.add(IPLabel);
        dialog.add(IPTextField);
        dialog.add(yourPortLabel);
        dialog.add(yourPortTextField);
        dialog.add(nameLabel);
        dialog.add(nameTextField);
        dialog.add(new Panel());
        JButton sureButton = new JButton("确定");
        dialog.add(sureButton);
        sureButton.addActionListener(new SureListener(IPTextField,yourPortTextField,nameTextField,dialog,config));
    }
    public void run()
    {
        DatagramPacket dp = null;
        DatagramSocket ds = null;
        byte[] buf = new byte[1024];
        try
        {
            dp = new DatagramPacket(buf,buf.length);
            ds = new DatagramSocket(myPort);
            System.out.println("myport="+myPort);
        }
        catch (Exception e)
        {}
        while (true)
        {
            try
            {
                ds.receive(dp);
                int length = dp.getLength();
                InetAddress address = dp.getAddress();
                int port = dp.getPort();
                String message = new String(dp.getData(),0,length);
                showArea.append("收到数据长度:"+length+"\n");
                showArea.append("收到数据来自:"+address+"端口:"+port+"\n");
                showArea.append("收到数据是:"+message+"\n");
            }
            catch (Exception e)
            {
            }
        }        
    }
}

//发送事件
class SendListener implements ActionListener
{
    private JFrame QQFrame;
    private JTextArea sendArea;
    private JTextArea showArea;
    private Config config;
    SendListener(JFrame in_QQFrame, JTextArea in_sendArea, JTextArea in_showArea, Config in_config)
    {
        QQFrame = in_QQFrame;
        sendArea = in_sendArea;
        showArea =in_showArea;
        config = in_config;
        System.out.println("in_port:"+config.getyourPort());
        QQFrame.validate();
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println("你点击了发送按钮");
        byte[] buf = sendArea.getText().trim().getBytes();
        try
        {
            InetAddress address = InetAddress.getByName(config.getIP());
            DatagramPacket dp = new DatagramPacket(buf,buf.length,address,Integer.parseInt(config.getyourPort()));
            DatagramSocket ds = new DatagramSocket();
            showArea.append("数据包目标地址:"+dp.getAddress()+"\n");
            showArea.append("数据包目标端口号:"+dp.getPort()+"\n");
            showArea.append("数据包长度:"+dp.getLength()+"\n");
            ds.send(dp);
        }
        catch (Exception ee) {}
    }
}

//配置设置
class SetDialog implements ActionListener
{
    private JDialog dialog;
    private JFrame frame;
    SetDialog(JDialog in_dialog,JFrame parent)
    {
        dialog = in_dialog;
        frame = parent;
    }
    public void actionPerformed(ActionEvent e)
    {
        dialog.validate();
        int x = frame.getX();
        int y = frame.getY();
        dialog.setBounds(x+50,y+50,200,250);
        dialog.setVisible(true);
    }
}
//关于对话框
class AboutDialog extends JDialog implements ActionListener
{
    private JFrame frame;
    private JLabel jl = new JLabel("感谢您的使用,作者:涵曦");
    AboutDialog(JFrame parent)
    {
        frame = parent;
        add(jl);
    }
    public void actionPerformed(ActionEvent e)
    {
        validate();
        int x = frame.getX();
        int y = frame.getY();
        setBounds(x+50,y+50,200,250);
        setVisible(true);
    }
}
//对话框的确定按钮事件
class SureListener implements ActionListener
{
    JTextField IPTextField;
    JTextField yourPortTextField;
    JTextField nameTextField;
    private Dialog dialog;
    private Config config;
    
    SureListener(JTextField in_IPTextField,JTextField in_yourPortTextField,JTextField in_nameTextField,JDialog in_dialog,Config in_config)
    {
        IPTextField = in_IPTextField;
        yourPortTextField = in_yourPortTextField;
        nameTextField = in_nameTextField;
        dialog = in_dialog;
        config = in_config;
    }
    public void actionPerformed(ActionEvent e)
    {
        config.set(IPTextField.getText(),yourPortTextField.getText(),nameTextField.getText());
        System.out.println("config:yourPort:"+config.getyourPort());
        dialog.setVisible(false);
    }
}
//参数
class Config
{
    private String IP="127.0.0.1";
    private String yourPort="666";
    private String name="hanxi";

    public void set(String in_IP, String in_yourPort,String in_name)
    {
        IP = in_IP;
        yourPort = in_yourPort;
        name = in_name;
    }
    public String getIP()
    {
        return IP;
    }
    public String getyourPort()
    {
        return yourPort;
    }
    public String getName()
    {
        return name;
    }
}
技术分享

下面是主方法代码:

技术分享
public class QQ
{
    public static void main(String args[])
    {
        if (args.length == 0)
        {
            System.out.println("请在QQ后面输入接收数据的端口号(需要空格)!");
            System.exit(0);
        }
        System.out.println(Integer.parseInt(args[0]));
        QQFrame frame = new QQFrame(Integer.parseInt(args[0]));
    }
}
技术分享

设置端口和IP的图片

 

技术分享

下面是聊天界面

技术分享

 

基于UDP协议的聊天室(java实现)

标签:about   使用   参数配置   vax   pen   ext   append   getx   extends   

原文地址:http://www.cnblogs.com/lyx2018/p/7150601.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!