In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "the method of developing Java chat system based on UDP protocol". In the daily operation, I believe that many people have doubts about the method of developing Java chat system based on UDP protocol. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "the method of developing Java chat system based on UDP protocol". Next, please follow the editor to study!
Code presentation
Some Client classes are shown below.
/ * *-* sender * *-* / class Client extends Thread {private static String content; / / send content private static int port; / / send port private static int target; / / destination port private static byte [] addr / / destination ip address / * *-* constructor * *-* / public Client (String content,int port,int target,byte [] addr) {this.content=content; this.port=port; this.target=target; this.addr=addr } / * *-* sender function * *-* / public void run () {DatagramSocket client= null; try {/ / defines the DatagramSocket object client= new DatagramSocket (port) whose sender port is port } catch (SocketException e) {e.printStackTrace ();} String str=content; DatagramPacket packet= null; try {/ / defines a DatagramPacket object that encapsulates the sent data and the destination address target packet= new DatagramPacket (str.getBytes (), str.getBytes (). Length, InetAddress.getByAddress (addr), target). } catch (UnknownHostException e) {e.printStackTrace ();} try {/ / send data client.send (packet);} catch (IOException e) {e.printStackTrace ();} / close release resource client.close () }} / * *-* receiver * *-* / class Server extends Thread {private static JTextArea jt; / / dialog box private static int target / / destination port / *-Constructor-* / public Server (JTextArea jt,int target) {this.jt=jt; this.target=target;}
Some Server classes are shown below.
/ *-receiver-* / class Server extends Thread {private static JTextArea jt; / / dialog box private static int target / / destination port / *-Constructor-* / public Server (JTextArea jt,int target) {this.jt=jt; this.target=target } / *-receiver function-* / public void run () {String str; DatagramSocket server= null Try {/ / defines a receiving DatagramSocket object server = new DatagramSocket (target) with a port number of target;} catch (SocketException e) {e.printStackTrace ();} byte [] buf=new byte [1024]; / / defines a DatagramPacket Datagram object that encapsulates received data DatagramPacket packet=new DatagramPacket (buf, buf.length) While (true) {try {/ / waiting for data to be received, server.receive (packet) will be blocked before the data is received;} catch (IOException e) {e.printStackTrace () } / / use the DatagramPacket method to get the received information and convert it to a string type str=new String (packet.getData (), 0recoverable packet.getLength ()); / / print the received information to the dialog box jt.append ("received:" + str+ ");}
Some Main classes are shown below.
Public class Main {public static int port; public static int target; public static byte [] addr; public static void Login () {System.out.print ("Please enter port number:"); Scanner ScPort=new Scanner (System.in); port=ScPort.nextInt (); System.out.print ("Please enter destination port number 1:"); Scanner ScTarget=new Scanner (System.in); target=ScTarget.nextInt () System.out.print ("Please enter destination port number 2:"); Scanner ScTarget2=new Scanner (System.in); int target2=ScTarget2.nextInt (); System.out.print ("enter destination IP address:"); Scanner Scaddr=new Scanner (System.in) / *-Store the target IP in an array-- * / String ip=Scaddr.nextLine () / *-public String [] split (String regex): splits this string based on the matching of a given regular expression-- * / String [] ii=ip.split ("\.") / *-the IPv4 we use today is stored in a 32-bit binary number, which is divided into four 8-bit binary numbers (4 one-byte binary numbers) for memory convenience, separated by dots between each 8 bits. Every 8-bit binary number can be converted into a decimal number of 0,255, for example, 103.42.176.244, which is called the point representation-- * / byte [] addr=new byte [4]; for (int iTuno [String content=jte.getText ()) / *-create an action for the send button-- * / if (content.equals (")) {/ *- -determine whether the information is empty and if so The content prompted for input in the dialog box is empty, otherwise send-* / jt.append ("input information:" + content+ "") / *-the parameters that send the send box information to the destination dialog box and pass in the Client method are "send content, send port, destination port" respectively. Destination IP address "- * / try {new Thread (new Client (content,port,target,addr)," sending Service ") .start () } catch (Exception exception) {exception.printStackTrace ();}} else {jt.append ("what you typed is empty");} jte.setText (""); / / reset the send box} after the message is sent; JPanel jp=new JPanel () JLabel jl=new JLabel (chat message); jp.add (jl); jp.add (jte); jp.add (jb); jf.add (jt,BorderLayout.PAGE_START); jf.add (jp,BorderLayout.PAGE_END); / / display GUI interface jf.setVisible (true); return jt } public static void main (String [] args) throws Exception {Login ();}} at this point, the study on "the method of developing Java chat system based on UDP protocol" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.