UDP网络程序

Posted dulute

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
            }
        }
    }
}

 

以上是关于UDP网络程序的主要内容,如果未能解决你的问题,请参考以下文章

网络LinuxLinux网络编程-TCP,UDP套接字编程及代码示范

网络LinuxLinux网络编程-TCP,UDP套接字编程及代码示范

[Java网络编程]UDP通信程序练习

Java网络程序; (滞后问题)

JAVA网络编程知识学习

网络编程——UDP编程