1. 程式人生 > >UDP網絡程序

UDP網絡程序

erl get res iad implement ride ext blue sage

服務端

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網絡程序