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 realize the function of online consultation based on UDP multithread in Java

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to realize the function of online consultation based on UDP multithreading in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to realize the function of online consultation based on UDP multithreading in Java"!

1. Sending thread import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;// sending thread public class TalkSend implements Runnable {DatagramSocket socket = null; BufferedReader reader = null; private int formPort; / / its own port private String toIP; / / the IP private int toPort to which it is sent / / Port public TalkSend (int formPort, String toIP, int toPort) {this.formPort = formPort; this.toIP = toIP; this.toPort = toPort; try {socket = new DatagramSocket (formPort); reader = new BufferedReader (new InputStreamReader (System.in)); / / get console input} catch (SocketException e) {e.printStackTrace () } @ Override public void run () {while (true) {try {String data = reader.readLine (); / / obtained data byte [] datas = data.getBytes () / / convert to the required byte array / / package the data into a package DatagramPacket packet = new DatagramPacket (datas,0, datas.length,new InetSocketAddress (this.toIP,this.toPort)); socket.send (packet); / / send packet / / exit instruction if (packet.equals ("bye")) {break } catch (IOException e) {e.printStackTrace ();}} socket.close ();}} 2. The receiving thread import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;// receiving thread public class TalkReceive implements Runnable {DatagramSocket socket = null; / / is the same as the socket of the sending end, and the communication private int port; private String msgFrom; / / marks the source of information public TalkReceive (int port, String msgFrom) {this.port = port; this.msgFrom= msgFrom Try {socket = new DatagramSocket (port);} catch (SocketException e) {e.printStackTrace ();}} @ Override public void run () {while (true) {try {byte [] container = new byte [1024]; DatagramPacket packet = new DatagramPacket (container, 0, container.length) Socket.receive (packet); / / receive data byte [] data = packet.getData (); String receiveData = new String (data, 0, data.length); System.out.println (msgFrom+ ":" + receiveData); if (receiveData.equals ("bye")) {break } catch (IOException e) {e.printStackTrace ();}} socket.close ();}} 3. Simulated student public class TalkStudent {public static void main (String [] args) {new Thread (new TalkSend (7777, "localhost", 9999). Start (); new Thread (new TalkReceive (8888, "teacher"). Start ();}} 4. Simulation teacher public class TalkTeacher {public static void main (String [] args) {new Thread (new TalkSend (5555, "localhost", 8888)). Start (); new Thread (new TalkReceive (9999, "student"). Start ();} so far, I believe you have a deeper understanding of "how to achieve online consultation based on UDP multithreading in Java". 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