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 chat Mini Program based on UDP Protocol by java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you java how to achieve chat Mini Program based on UDP protocol, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

UDP (User Data Protocol, user Datagram Protocol) is the protocol corresponding to TCP. It is a connectionless protocol, it does not establish a connection with the other party, but sends the packet directly!

UDP is suitable for applications that transmit only a small amount of data at a time and do not require high reliability. Because UDP protocol has no connection process, its communication efficiency is high, but it is also because of this, its reliability is not as high as TCP protocol. QQ uses UDP to send messages, so sometimes there are situations where messages are not received.

Simulate chat Mini Program by sending and receiving UDP protocol

Create the A side of the chat program:

1. Send the message to the receiver

1. Information to be sent [keypad]

2. Specify the ip of the receiving end

3. Specify the port of the receiver

4. Package [create objects for DatagramPacket]

5. Send [create DatagramSocket object]

2. Receive the message of receiving reply

1. Prepare to receive using byte array

2. Encapsulate the array into a Datagram package [create an object for DatagramPacket]

3. Receive data

4. Parse the valid data of the packet

3. The above operation cycle operation

Create the B side of the chat program:

1. Receive the information from the sender

1. Prepare to receive using byte array

2. Encapsulate the array into a Datagram package [create an object for DatagramPacket]

3. Receive data [create an object for DatagramSocket]

4. Parse the valid data of the packet

2. Reply to the message of the sender

1. Information to be sent [keypad]

2. Specify the ip sent [parse the ip of the received packet]

3. Specify the port of the receiving end [just parse the port of the received packet]

4. Package [create objects for DatagramPacket]

5. Send

3. The above operation cycle operation

Port 1: package com.offcn.second; import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.util.Scanner; public class MyQQ {public static void main (String [] args) throws Exception {DatagramSocket ds = new DatagramSocket (); Scanner sc= new Scanner (System.in); while (true) {/ / send message System.out.print ("My:") / / enter sending information String smsg = sc.nextLine (); / / byte [] sbs = smsg.getBytes (); / / int slen = sbs.length / / create packet DatagramPacket sdp = new DatagramPacket (smsg.getBytes (), / / convert data to byte array smsg.getBytes () .length,// data length InetAddress.getLocalHost (), / / receive port number 9999) / / receiving end, port number / / sending ds.send (sdp); / / receiving messages / / creating packets for receiving data DatagramPacket rdp = new DatagramPacket (new byte [1024], 1024); / / receiving data ds.receive (rdp) / / convert the received data into the string String rmsg = new String (rdp.getData (), 0dp.getLength ()); / / printout System.out.println ("His:" + rmsg);}} Port 2: package com.offcn.second; import java.net.DatagramPacket;import java.net.DatagramSocket;import java.util.Scanner Public class HisQQ {public static void main (String [] args) throws Exception {DatagramSocket ds = new DatagramSocket (9999); Scanner sc = new Scanner (System.in); while (true) {/ / create packets DatagramPacket rdp = new DatagramPacket (new byte [1024], 1024); ds.receive (rdp) / / convert the received data into the string String rmsg = new String (rdp.getData (), 0dp.getLength ()); / / output the received information System.out.println ("His:" + rmsg); / / send a message back to the other party / / enter the data sent System.out.print ("My:") String smsg = sc.nextLine (); / / create the packet sending data DatagramPacket sdp = new DatagramPacket (smsg.getBytes (), smsg.getBytes () .length, rdp.getAddress (), rdp.getPort ()); ds.send (sdp);}

Running result:

The above is all the contents of the article "how java implements chat Mini Program based on UDP protocol". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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