java UDP通信客户端与服务器端实例分析
2020-04-08 12:07
417 查看
本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《Java Socket编程技巧总结》、《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- java udp网络编程及实例代码,实现客户端和服务端通信
- java使用UDP来进行客户端和服务器端通信的简单例子
- java使用UDP来进行客户端和服务器端通信的简单例子
- java Tcp通信客户端与服务器端实例
- python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
- python网络编程之UDP通信实例(含服务器端、客户端、UDP广播例子)
- Java服务器端List对象转换为JSON对象并返回客户端实例
- UDP的客户端和服务器端的实例(VC6.0实现)
- Linux C+gsoap/Win C#服务器端与Java客户端通信
- java-基本的Socket编程-实现服务器端和客户端通信
- java.UDP通信(实现服务端与客户端简单的聊天)
- Java服务器端List对象转换为JSON对象并返回客户端实例
- 【JAVA】【NIO】对系列翻译的总结,一个综合实例的分析,网络服务端接收客户端输入,实时将内容写文件
- 下联网关国标对接中的通信过程分析和实例之客户端主动发起的实时音视频点播过程
- java模拟TCP通信实现客户端上传文件到服务器端
- Java Socket学习:客户端与服务器端通信
- QT5 下UDP 编程实例:客户端,服务器端
- java UDP网络通信实例
- Linux socket编程入门及客户端服务器端通信实现 – 提高篇:TCP连接过程分析