• java编程基础总结——32.UDP网络编程


    DatagramSocket

    DatagramSocket这个接受和发送数据报

     

     UDP网络协议下的Socket对象:

    UDP(User Datagram Protocol):  用户数据报协议

    追求速度的网络协议

    特点:1. 无连接(不需要知道发送方的IP)

               2. 不可靠(只管发送,不管接收)

               3. 不安全
     

    创建UDP客户端程序的流程:

    1. 创建客户端套接字

    2. 发送 / 接收数据

    3. 关闭套接字

    datagramSocket是java封装的基于UDP网络协议的套接字对象

    DatagramPacket进行数据的封装和解封装

    1. UDP发送数据案例:

    1. package com.openlab.day27;
    2. import java.io.IOException;
    3. import java.net.*;
    4. public class TestUDP {
    5. public static void main(String[] args) {
    6. DatagramSocket datagramSocket = null;
    7. try {
    8. // 注意,创建一个udp socket对象,默认占据了8080
    9. datagramSocket = new DatagramSocket(8080);
    10. String msg = "你好啊,socket对象";
    11. byte[] bytes = msg.getBytes();
    12. // 数据报对象
    13. DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("127.0.0.1", 8888));
    14. // 发送数据
    15. datagramSocket.send(packet);
    16. System.out.println("数据已经成功发送");
    17. } catch (IOException e) {
    18. e.printStackTrace();
    19. } finally {
    20. datagramSocket.close();
    21. }
    22. }
    23. }

    2. UDP发送、接收数据案例:

    发送:

    1. package com.openlab.day27;
    2. import java.io.IOException;
    3. import java.net.DatagramPacket;
    4. import java.net.DatagramSocket;
    5. import java.net.InetSocketAddress;
    6. import java.net.SocketException;
    7. import java.util.Scanner;
    8. public class TestUDP03 {
    9. public static final String DES_IP = "127.0.0.1";
    10. public static final int DES_PORT = 8888;
    11. public static void main(String[] args) {
    12. DatagramSocket socket = null;
    13. Scanner sc = null;
    14. try {
    15. socket = new DatagramSocket(9999);
    16. sc = new Scanner(System.in);
    17. while (true) {
    18. System.out.print("请输入您要发送的数据:");
    19. String msg = sc.nextLine();
    20. byte[] buf = msg.getBytes();
    21. InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
    22. DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
    23. socket.send(pd);
    24. System.out.println("数据发送成功!!");
    25. }
    26. } catch (SocketException e) {
    27. e.printStackTrace();
    28. } catch (IOException e) {
    29. e.printStackTrace();
    30. } finally {
    31. socket.close();
    32. }
    33. }
    34. }

    接收:

    1. package com.openlab.day27;
    2. import java.io.IOException;
    3. import java.net.DatagramPacket;
    4. import java.net.DatagramSocket;
    5. import java.net.DatagramSocketImpl;
    6. import java.net.SocketException;
    7. public class TestUDP02 {
    8. public static void main(String[] args) {
    9. DatagramSocket socket = null;
    10. try {
    11. socket = new DatagramSocket(8888);
    12. while (true) {
    13. byte[] buf = new byte[1024];
    14. DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
    15. System.out.println("正在等待客户端发送数据……");
    16. // 等待接受数据
    17. socket.receive(pd);
    18. System.out.println("接收到数据……");
    19. // 将数据转换出来
    20. byte[] msg = pd.getData();
    21. String sendIP = pd.getAddress().getHostAddress();
    22. int sendPort = pd.getPort();
    23. System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
    24. }
    25. } catch (SocketException e) {
    26. e.printStackTrace();
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. } finally {
    30. socket.close();
    31. }
    32. }
    33. }

    3. UDP使用多线程实现收发数据(既能接收数据又能发送数据)

    每个启动两个线程,主线程充当一个线程,自己再写一个线程,一个线程用来接收数据,一个线程用来发送数据。模拟两个人,只需改变IP,端口即可互相通信

    第一个:

    1. package com.openlab.day27;
    2. import java.io.IOException;
    3. import java.net.DatagramPacket;
    4. import java.net.DatagramSocket;
    5. import java.net.InetSocketAddress;
    6. import java.net.SocketException;
    7. import java.util.Scanner;
    8. public class TestUDPSever02 {
    9. public static final String DES_IP = "127.0.0.1";
    10. public static final int DES_PORT = 9999;
    11. public static void main(String[] args) {
    12. DatagramSocket socket = null;
    13. Scanner sc = null;
    14. try {
    15. socket = new DatagramSocket(8888);
    16. // 启动一个接收到线程,来进行数据接受
    17. UDPRreceiveMsg02 udpRreceiveMsg = new UDPRreceiveMsg02(socket);
    18. udpRreceiveMsg.start();
    19. sc = new Scanner(System.in);
    20. while (true) {
    21. System.out.print(">");
    22. String msg = sc.nextLine();
    23. byte[] buf = msg.getBytes();
    24. InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
    25. DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
    26. socket.send(pd);
    27. }
    28. } catch (SocketException e) {
    29. e.printStackTrace();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. } finally {
    33. socket.close();
    34. }
    35. }
    36. }
    37. class UDPRreceiveMsg02 extends Thread {
    38. private DatagramSocket socket;
    39. public UDPRreceiveMsg02(DatagramSocket socket) {
    40. this.socket = socket;
    41. }
    42. @Override
    43. public void run() {
    44. try {
    45. receiveMsg();
    46. } catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. private void receiveMsg() throws IOException {
    51. while (true) {
    52. byte[] buf = new byte[1024];
    53. DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
    54. // 等待接受数据
    55. socket.receive(pd);
    56. // 将数据转换出来
    57. byte[] msg = pd.getData();
    58. String sendIP = pd.getAddress().getHostAddress();
    59. int sendPort = pd.getPort();
    60. System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
    61. }
    62. }
    63. }

    第二个:

    1. package com.openlab.day27;
    2. import java.io.IOException;
    3. import java.net.DatagramPacket;
    4. import java.net.DatagramSocket;
    5. import java.net.InetSocketAddress;
    6. import java.net.SocketException;
    7. import java.util.Scanner;
    8. public class TestUDPSever {
    9. public static final String DES_IP = "171.211.191.190";
    10. public static final int DES_PORT = 12340;
    11. public static void main(String[] args) {
    12. DatagramSocket socket = null;
    13. Scanner sc = null;
    14. try {
    15. socket = new DatagramSocket(9999);
    16. // 启动一个接收的线程,来进行数据接收
    17. UDPRreceiveMsg udpRreceiveMsg = new UDPRreceiveMsg(socket);
    18. udpRreceiveMsg.start();
    19. sc = new Scanner(System.in);
    20. while (true) {
    21. System.out.print(">");
    22. String msg = sc.nextLine();
    23. byte[] buf = msg.getBytes();
    24. InetSocketAddress address = new InetSocketAddress(DES_IP, DES_PORT);
    25. DatagramPacket pd = new DatagramPacket(buf,0, buf.length, address);
    26. socket.send(pd);
    27. }
    28. } catch (SocketException e) {
    29. e.printStackTrace();
    30. } catch (IOException e) {
    31. e.printStackTrace();
    32. } finally {
    33. socket.close();
    34. }
    35. }
    36. }
    37. class UDPRreceiveMsg extends Thread {
    38. private DatagramSocket socket;
    39. public UDPRreceiveMsg(DatagramSocket socket) {
    40. this.socket = socket;
    41. }
    42. @Override
    43. public void run() {
    44. try {
    45. receiveMsg();
    46. } catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. private void receiveMsg() throws IOException {
    51. while (true) {
    52. byte[] buf = new byte[1024];
    53. DatagramPacket pd = new DatagramPacket(buf, 0, buf.length);
    54. // 等待接受数据
    55. socket.receive(pd);
    56. // 将数据转换出来
    57. byte[] msg = pd.getData();
    58. String sendIP = pd.getAddress().getHostAddress();
    59. int sendPort = pd.getPort();
    60. System.out.println("接受到"+ sendIP +":"+ sendPort +"发送过来的数据,数据是:"+ new String(msg));
    61. }
    62. }
    63. }
  • 相关阅读:
    uniapp map地图实现marker聚合点,并点击marker触发事件
    二维数组与稀疏数组的互转实现与写入写出
    【深度学习21天学习挑战赛】9、生成对抗网络(GAN)手写数字生成
    【文末送书】Python数据分析
    两个月雅思口语速成
    基于BP神经网络识别手写字体MINST字符集
    APP自动化测试-Appium元素定位之元素等待
    REDIS00_SpringBoot整合redis、RedisTemplate使用、工具类的抽取
    android:can not find libdevapi.so
    轻松入门Spring MVC
  • 原文地址:https://blog.csdn.net/m0_58679504/article/details/126448365