• UDP网络程序


    服务端

    public class Weather extends Thread{
    
        String weather = "节目预报,八点有大型晚会,请收听";
        int port = 9898;
        InetAddress iaddress = null;
        MulticastSocket socket = null;
        Weather() {
            try {
                iaddress = InetAddress.getByName("224.255.10.0");
                socket = new MulticastSocket(port);
                socket.setTimeToLive(1);
                socket.joinGroup(iaddress);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                DatagramPacket packet = null;
                byte data[] = weather.getBytes();
                packet = new DatagramPacket(data, data.length,iaddress,port);
                System.out.println(new String(data));
                try {
                    socket.send(packet);
                    sleep(3000);
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Weather w = new Weather();
            w.start();
        }
    
    }

    客户端

    public class Receive extends JFrame implements  Runnable ,ActionListener{
    
        int port;
        InetAddress group = null;
        MulticastSocket socket = null;
        JButton ince = new JButton("开始接收");
        JButton stop = new JButton("停止接收"); 
        JTextArea inceAr = new JTextArea(10,10);
        JTextArea inced = new JTextArea(10,10);
        Thread thread ;
        boolean b = false ;
        
        public Receive() {
            // TODO Auto-generated constructor stub
            super("广播数据报");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            thread = new Thread(this);
            ince.addActionListener(this);
            stop.addActionListener(this);
            inceAr.setForeground(Color.blue);
            JPanel north =  new JPanel();
            north.add(ince);
            north.add(stop);
            add(north,BorderLayout.NORTH);
            JPanel center = new JPanel();
            center.setLayout(new GridLayout(1, 2));
            center.add(inceAr);
            center.add(inced);
            add(center, BorderLayout.CENTER);
            validate();
            port = 9898;
            try {
                group = InetAddress.getByName("224.255.10.0");
                socket = new MulticastSocket(port);
                socket.joinGroup(group);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Receive rec = new Receive();
            rec.setSize(460,200);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource() == ince){
                ince.setBackground(Color.red);
                stop.setBackground(Color.yellow);
                if(!(thread.isAlive())){
                    thread = new Thread(this);
                }
                thread.start();
                b = false;
            }
            if(e.getSource() == stop){
                ince.setBackground(Color.yellow);
                stop.setBackground(Color.red);
                b = true;
            }
        }
    
        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                byte data[] = new byte[1024];
                DatagramPacket packet = null;
                packet = new DatagramPacket(data, data.length,group,port);
                try {
                    socket.receive(packet);
                    String message = new String(packet.getData(), 0, packet.getLength());
                    inceAr.setText("正在接收内容:
    " + message);
                    inced.append(message + "
    ");
                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
                if(b==true){
                    break;
                }
            }
        }
    }
  • 相关阅读:
    Sublime Text 无法使用Package Control或插件安装失败的解决方法
    phpstorm破解
    require
    在线支付
    解决华为手机用rem单位,内容超出屏幕宽度问题
    JS如何判断是不是iphoneX
    iPhoneX页面安全区域与内容重叠问题
    .NET 大数据量并发解决方案
    js 弹出div窗口 可移动 可关闭
    colgroup 整行变色
  • 原文地址:https://www.cnblogs.com/dulute/p/10661408.html
Copyright © 2020-2023  润新知