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/03 Report--
This article introduces the Java Socket server key code configuration example analysis, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Java Socket server will have more or less problems when many people use it. In fact, there are many problems in the key code that we need to deal with. Let's take a look at how to better use the relevant code of Java Socket server.
Procedure2 adds multithreading compared to procedure1. One thread is responsible for receiving messages and the other is responsible for sending messages. When the receiving thread on the server side cannot send the message because of readUTF (), the thread responsible for sending the message allows the server side to send the message as well. The same principle is true of the client.
The key codes added on the server side are as follows:
Class ServerReadThread extends Thread {private DataInputStream dis; public ServerReadThread (DataInputStream dis) {this.dis = dis;} public void run () {String info; try {while (true) {info = dis.readUTF (); System.out.println ("client says:" + info); if (info.equals ("goodbye")) {System.out.println ("client bye!"); System.exit (0);} catch (IOException e) {e.printStackTrace () } class ServerWriteThread extends Thread {private DataOutputStream dos; private BufferedReader br; public ServerWriteThread (DataOutputStream dos,BufferedReader br) {this.dos = dos; this.br = br;} public void run () {String info; try {while (true) {info = br.readLine (); dos.writeUTF (info); if (info.equals ("bye")) {System.exit (0);} catch (IOException e) {e.printStackTrace ();}
The accepted class ServerReadThread inherits Thread and constructs a constructor for the parameters of the DataInputStream object to receive information from each other. The ServerWriteThread class responsible for sending inherits Thread and constructs a two-parameter constructor of the DataInputStream object and the BufferedRead object, which is responsible for sending messages. New ServerReadThread (dis) .start (); new ServerWriteThread (dos,br) .start (); start two threads. The principle of the client side is similar to that of the server side, so it will not be explained in more detail.
Thinking: procedure2 compared to procedure1 has achieved multi-threaded chat, although only running on the console, but although the sparrow is small, complete, which has reflected the core idea of java Socket programming and multithreading, the purpose of this course design has been achieved. In order to achieve a more user-friendly effect in Java Socket server, I introduced awt, a component of javaGUI, and also introduced related technologies, such as event-based drivers and calls between components of awt. At the same time, the distribution and organizational structure of the code should be adjusted accordingly.
This is the end of the case analysis of the key code configuration of the Java Socket server. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
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.