码迷,mamicode.com
首页 > 其他好文 > 详细

UDP网络程序

时间:2019-04-06 14:20:24      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:stop   while   ast   form   bytes   sts   star   exce   eve   

服务端

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("正在接收内容:\n" + message);
                inced.append(message + "\n");
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
            if(b==true){
                break;
            }
        }
    }
}

 

UDP网络程序

标签:stop   while   ast   form   bytes   sts   star   exce   eve   

原文地址:https://www.cnblogs.com/dulute/p/10661408.html

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