Java最简单的UDP收发数据的例子

来源:互联网 发布:网络奇葩歌词找歌 编辑:程序博客网 时间:2024/05/18 02:41

程序1、2具备收发数据的示例。

  • 发送端需要设置IP地址和端口,而接收端只需要设置端口
  • 程序1的发送端口与程序2的接收端端口一致,反之亦然
  • 主要两个发送或接收时用的端口不能重复

程序1:

public class J {    public static void main(String[] args) {        send();        receiver();    }    public static void send() {        new Thread() {            public void run() {                DatagramSocket ds = null;                try {                    // 定义一个UDP的Socket来发送数据                    ds = new DatagramSocket();                    String hello = "JJJJJJJJJJJJJJJJ";                    /*                     *  定义一个UDP的数据发送包来发送数据,inetSocketAddress表示要接收的地址。                     *                       *  IP地址和端口都需要设置                     */                    InetSocketAddress address = new InetSocketAddress("127.0.0.1", 3000);                    DatagramPacket dp = new DatagramPacket(hello.getBytes(), hello.getBytes().length, address);                    while (true) {                        ds.send(dp);                        Thread.sleep(1000);                    }                } catch (Exception e) {                    e.printStackTrace();                } finally {                    if (ds != null) {                        ds.close();                    }                }            };        }.start();    }    public static void receiver() {        new Thread() {            @Override            public void run() {                // UDP接收端                DatagramSocket ds = null;                try {                    /*                     * 接收端只需要端口,注意与发送端保持一致                     */                    ds = new DatagramSocket(2000);                    // 定义将UDP的数据包接收到什么地方                    byte[] buf = new byte[1024];                    // 定义UDP的数据接收包                    DatagramPacket dp = new DatagramPacket(buf, buf.length);                    while (true) {                        // 接收数据包                        ds.receive(dp);                        String string = new String(dp.getData(), 0, dp.getLength());                        System.out.println("Java UDP:" + dp.getLength() + "->" + string);                    }                } catch (IOException e) {                    e.printStackTrace();                } finally {                    if (ds != null) {                        ds.close();                    }                }            }        }.start();    }}

程序2:

import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;public class C {    public static void main(String[] args) {        send();        receiver();    }    public static void send() {        new Thread() {            public void run() {                DatagramSocket ds = null;                InetSocketAddress address = null;                try {                    /*                     * IP地址和端口,注意端口不要重复                     */                    address = new InetSocketAddress("127.0.0.1", 2000);                    // 定义一个UDP的Socket来发送数据                    ds = new DatagramSocket();                    String hello = "CCCCCCCCCCCCCCCCCCCCCCCCC";                    // 定义一个UDP的数据发送包来发送数据,inetSocketAddress表示要接收的地址                    DatagramPacket dp = new DatagramPacket(hello.getBytes(), hello.getBytes().length, address);                    while (true) {                        ds.send(dp);                        Thread.sleep(1000);                    }                } catch (Exception e) {                    e.printStackTrace();                } finally {                    if (ds != null) {                        ds.close();                    }                }            };        }.start();    }    public static void receiver() {        new Thread() {            @Override            public void run() {                // UDP接收端                DatagramSocket ds = null;                try {                    /*                     * 这里只需要设置端口,无需IP地址。其端口与发送端一致。                     */                    ds = new DatagramSocket(3000);                    // 定义将UDP的数据包接收到什么地方                    byte[] buf = new byte[1024];                    // 定义UDP的数据接收包                    DatagramPacket dp = new DatagramPacket(buf, buf.length);                    while (true) {                        // 接收数据包                        ds.receive(dp);                        String string = new String(dp.getData(), 0, dp.getLength());                        System.out.println("Java UDP:" + dp.getLength() + "->" + string);                    }                } catch (IOException e) {                    e.printStackTrace();                } finally {                    if (ds != null) {                        ds.close();                    }                }            }        }.start();    }}
原创粉丝点击