Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of key Code configuration of Java Socket Server

Shulou Source: shulou.com Published: 2022-06-03 01:04:45 10月04日 Update

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.

Tags: Server service thread code message key client client object instance instance analysis analysis configuration content function principle parameter more component problem Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS vpn Shulou Tech Info Xiaomi