Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand java UDP communication client and server

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand java UDP communication client and server". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand java UDP communication client and server".

At first, Udp was transmitted in bytes, so it was very limited.

Server side:

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 ();}

Client:

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 (); / /}

Note: since the transmission must be in bytes, the Udp transfer uses something of a container class to receive bytes.

First create a byte array, and then create a container with this array. Used to transmit data.

Example: transfer data of type Long

Server side:

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 ());}

Client:

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 ();}}

Note: because Udp is transmitted in bytes, the input and output streams of ByteArray are used for data conversion.

In addition, compared to the Output stream, the Input stream needs an array as a parameter to hold the data when it is built.

On the basis of the basic Udp transmission, the code is divided into two parts, one is to convert the transmitted or accepted Long type data into byte type data, and then the basic data transmission.

On the other hand, direct byte streams cannot be converted to Long types. Similarly, the data just received is of byte type, and direct printing (System.out.println) is output as a string, which needs to be converted through the data stream of Data.

At this point, I believe you have a deeper understanding of "how to understand java UDP communication client and server". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report