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 implement UDP with java

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

Share

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

This article mainly introduces java how to achieve UDP, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

UDP:

DatagramSocket is required for intermediate transportation

Connectionless transport layer protocol that provides simple, unreliable transaction-oriented information transmission services that may be lost, very simple, and low overhead

Everything is centered on the bag.

Data transmission does not use IO stream

Receiving end

Address already in use: Port conflicts are not allowed under the same Cannot bind protocol

1. Create the receiving end using the designated port of DatagramSocket

2. Prepare the container to be packaged into a DatagramPacket package

3. Blocking acceptance package receive (DatagramPacket p)

4. The analysis data getData () returns a byte array of type, getLength () returns the data length, and the type is int

5. Release resources

Public class http {public static void main (String [] args) throws Exception {System.out.println ("receiver initiating..."); / 1. Create receiver DatagramSocket server=new DatagramSocket (9999) using DatagramSocket designated port; / / do not conflict with ports on the same computer / / 2, prepare the container to be packaged into DatagramPacket package byte [] container=new byte [1024x60]; DatagramPacket packet=new DatagramPacket (container,0,container.length) / / 3. Blocking acceptance package receive (DatagramPacket p) Server.receive (packet); / / blocking, waiting time / / 4, analysis data getData () return type is byte array, getLength () returns data length, type is int byte [] datas=packet.getData (); int len=packet.getLength (); System.out.println (new String (datas,0,datas.length)); / / 5, release resource server.close ();}}

Sending end

1. Use the designated port of DatagramSocket to create the sender

2. To prepare the data, be sure to convert it to a byte array

3. Prepare the container to be encapsulated into a DatagramPacket package. You need to specify the destination (ip address and port).

4. Send the parcel send (DatagramPacket p)

5. Release resources

Public class client {public static void main (String [] args) throws IOException {System.out.println ("sending starts..."); / / 1. Create the sender using the DatagramSocket designated port DatagramSocket client = new DatagramSocket (8888); / / 2. To prepare the data, be sure to switch to the byte array String data= "I am the most handsome"; byte [] datas=data.getBytes () / / 3. To prepare the container to be packaged into a DatagramPacket package, you need to specify the destination (ip address and port) DatagramPacket packet=new DatagramPacket (datas,0,datas.length,new InetSocketAddress ("localhost", 9999)); / / 4. Send the package send (DatagramPacket p) Client.send (packet); / / 5, release resources client.close ();}} Thank you for reading this article carefully. I hope the article "how to achieve UDP in java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you 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