欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java中使用UDP协议的Socket通信

程序员文章站 2022-07-03 20:05:05
Java中使用UDP协议的Socket通信:基本测试代码:客户端:package udpsocket;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class Demo1_Send { public static void main(String[] args) throws Exception { // 在发生数据之...

Java中使用UDP协议的Socket通信:

基本测试代码:
客户端:

package udpsocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Demo1_Send {
    public static void main(String[] args) throws Exception {

        // 在发生数据之前,你得先把数据准备好,发生目标的IP地址,端口

        // 准备一个字符串数据,待发送
        String sendMsg = "你在做什么呢?";

        // 获取目标主机的IP地址
        InetAddress hostIP = InetAddress.getByName("127.0.0.1");
        // 获取目标主机的端口号
        int hostPort = 6666;

        // 创建DatagramSocket对象,相当于创建码头
        DatagramSocket socket = new DatagramSocket();

        // 创建DatagramPacket对象,相当于创建船只,参数为:发数据的字节码,字节码的长度,目标IP地址,目标端口号
        DatagramPacket packet = new DatagramPacket(sendMsg.getBytes(), sendMsg.getBytes().length, hostIP, hostPort);

        // 开始发送数据,在码头护送包装数据的船只
        socket.send(packet);

        // 发送完毕后将socket关闭,码头关门
        socket.close();
    }
}

服务器端:

package udpsocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo1_Receive {
    public static void main(String[] args) throws Exception {

        // 创建DatagramSocket对象,并指定端口号
        DatagramSocket socket = new DatagramSocket(6666);

        // 创建DatagramPacket对象,并指定接收容器,及其长度
        DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);

        // 开始接收数据
        socket.receive(packet);

        // 取出数据
        byte[] msgArr = packet.getData();
        // 获取有效数据的长度
        int length = packet.getLength();

        // 将字节码还原成字符串
        String msgStr = new String(msgArr, 0, length);
        System.out.println(msgStr);
    }
}

改进后的代码:

客户端:

package udpsocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class Demo2_Send {
    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);

        DatagramSocket socket = new DatagramSocket();

        InetAddress hostIP = InetAddress.getByName("127.0.0.1");
        int hostPort = 7777;

        System.out.println("请输入发送信息,输入over结束:");
        while (sc.hasNext()) {
            String msgStr = sc.nextLine();
            if ("over".equals(msgStr))  break;

            DatagramPacket packet = new DatagramPacket(msgStr.getBytes(), msgStr.getBytes().length, hostIP, hostPort);
            socket.send(packet);
        }

        socket.close();
    }
}

服务器端:

package udpsocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo2_Receive {
    public static void main(String[] args) throws Exception {

        DatagramSocket socket = new DatagramSocket(7777);

        while (true) {
            DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
            socket.receive(packet);

            // 取出数据
            byte[] msgArr = packet.getData();
            int length = packet.getLength();
            String msgStr = new String(msgArr, 0, length);

            // 获取发送者的IP地址,端口号
            String srcIP = packet.getAddress().getHostAddress();
            int srcPort = packet.getPort();

            System.out.println(srcIP + "-" + srcPort + ":" + msgStr);
        }
    }
}

本文地址:https://blog.csdn.net/pipizhen_/article/details/107673114

相关标签: JavaSE