java中UDP通信客户端与服务器端的示例分析

发布时间:2021-08-23 10:35:26 作者:小新
来源:亿速云 阅读:113

小编给大家分享一下java中UDP通信客户端与服务器端的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

具体如下:

最初Udp是以字节为单位进行传输的,所以有很大的限制

服务器端:

import java.net.*;
public class TestUdpServer {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf,buf.length);
//        try {
            DatagramSocket ds = new DatagramSocket(2345);
            while(true) {
                ds.receive(dp);
                System.out.println(new String(buf,0,dp.getLength()));    
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
}

用户端:

import java.net.*;
public class TestUdpClient {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        buf = (new String("hello")).getBytes();
        DatagramPacket dp = new DatagramPacket(buf,buf.length,new InetSocketAddress("127.0.0.1",2345));
//        try {
            DatagramSocket ds = new DatagramSocket(5679);
            ds.send(dp);
            ds.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}

注:由于必须以字节为单位进行传输,Udp的传输用了一个容器类的东西,用来接收字节

先建一个字节数组,然后以这个数组创建容器。用来传输数据。

实例:传输一个Long类型的数据

服务器端:

import java.io.*;
import java.net.*;
public class UdpServer {
    public static void main(String[] args) throws Exception {
        byte[] buf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buf,buf.length);
        DatagramSocket ds = new DatagramSocket(2345);
        while(true) {
            ByteArrayInputStream is = new ByteArrayInputStream(buf);
            DataInputStream dis = new DataInputStream(is);
            ds.receive(dp);
            System.out.println(dis.readLong());    
        }
    }
}

用户端:

import java.io.*;
import java.net.*;
public class UdpClient {
    public static void main(String[] args) throws Exception {
        Long n = 10000L;
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeLong(n);
        byte[] buf = new byte[1024];
        buf = os.toByteArray();
        System.out.println(buf.length);
        DatagramPacket dp = new DatagramPacket(buf,buf.length,
                new InetSocketAddress("127.0.0.1",2345));
        DatagramSocket ds = new DatagramSocket(5679);
        ds.send(dp);
        ds.close();
    }
}

注:由于Udp是以字节为单位进行传输的,所以要用到ByteArray的输入和输出流用来进行数据的转换。

另外,相较于Output流,Input流在构建的时候需要一个数组作为参数,用来存放数据。

在基本的Udp传输的基础上,代码分为两部分,一部分是把传输或接受的Long类型数据转换为byte类型的数据,然后是基本的数据传输。

另一方面,直接的字节流不能转换为Long类型,同理,刚接收的数据是字节类型,直接打印(System.out.println)是以字符串类型输出的,都需要通过Data的数据流进行转换。

以上是“java中UDP通信客户端与服务器端的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. java实现UDP通信
  2. java中的UDP通信是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java udp

上一篇:如何解决tensoflow在已训练模型上继续训练fineturn的问题

下一篇:SpringSecurity如何实现默认表单登录页展示

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》