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

What is the principle of java UDP communication?

2025-02-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the principle of java UDP communication". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Principle

1. UDP protocol is an unreliable network protocol, which establishes a Socket object at each end of the communication, but these two Socket are just objects that send and receive data.

2. For both sides of the communication based on UDP protocol, there is no so-called client-side and server-side concept.

Java provides DatagramSocket classes as Socket based on UDP protocol

Example

Package test; import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress; / * * UDPCLient deom * @ author * / public class UDPClient {public static void main (String [] args) throws Exception {DatagramSocket clientSocket = new DatagramSocket (); BufferedReader inFromUser = new BufferedReader (new InputStreamReader (System.in)) / / get the local IP address InetAddress IPAddress = InetAddress.getLocalHost (); byte [] sendData; byte [] receiveData = new byte [1024]; System.out.println ("Please enter an English sentence and the server will return its uppercase form (enter exit to exit)"); while (true) {String sentence = inFromUser.readLine () If (sentence.equals ("exit")) break; sendData = sentence.getBytes (); / / create a send Datagram packet with source address #, destination address # DatagramPacket sendPacket = new DatagramPacket (sendData, sendData.length, IPAddress, 9876); / / send Datagram packet clientSocket.send (sendPacket) / / create a receive Datagram packet DatagramPacket receivePacket = new DatagramPacket (receiveData, receiveData.length); / / receive the Datagram packet clientSocket.receive (receivePacket) of the receiving server; String modifiedSentence = new String (receivePacket.getData ()); System.out.println ("FROM SERVER:" + modifiedSentence);} clientSocket.close () This is the end of the content of "what is the principle of java UDP communication". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report