In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Java realizes the function of chat room based on UDP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Project structure
Data.java
Package udp;import java.net.InetAddress;public class data {InetAddress Address; int Port; public InetAddress getAddress () {return Address;} public void setAddress (InetAddress address) {Address = address;} public int getPort () {return Port;} public void setPort (int port) {Port = port;}} server side
Server.java
Package udp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.util.ArrayList;public class Server {DatagramSocket socket = null; ArrayList client; public Server () {try {socket = new DatagramSocket (8888);} catch (SocketException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} client = new ArrayList () } public void sprinter () {try {while (true) {byte [] buf = new byte [3000]; / / packets receiving data DatagramPacket packet = new DatagramPacket (buf,buf.length); socket.receive (packet); / / address InetAddress clientAddress = packet.getAddress (); / / port number int clientPort = packet.getPort (); data d = new data (); d.setAddress (clientAddress) D.setPort (clientPort); int iTun0; / / determine whether there is a client for (; I) sending new messages in the client collection.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.