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 deeply analyze the design principle of Java Socket sharing

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I will talk to you about how to deeply dialyze the design principle of Java Socket sharing, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

There are many problems we need to solve when using Java Socket sharing. In fact, many problems can be found in the source code. Let's take a look at how to make better use of the relevant Java Socket sharing services.

In the actual network environment, it is not feasible to serve only one user at a time. In addition to processing the input information of users, an excellent network service program must also be able to respond to connection requests from multiple clients at the same time. In Java Socket sharing, it is very easy to achieve the above features.

The design principles of Java Socket sharing:

The main program listens on a port and waits for the customer to access; at the same time, it constructs a thread class, ready to take over the session. When a Socket session is generated, the session is handed over to the thread, and the main program continues to listen. It is a good idea to use the Thread class or Runnable interface to implement it.

{implement message sharing}

Import java.io.*; import java.net.*; public class Server extends ServerSocket {private static final int SERVER_PORT = 10000; public Server () throws IOException {super (SERVER_PORT); try {while (true) {Socket socket = accept (); new CreateServerThread (socket);}} catch (IOException e) {} finally {close ();}} /-CreateServerThread class CreateServerThread extends Thread {private Socket client Private BufferedReader in; private PrintWriter out; public CreateServerThread (Socket s) throws IOException {client = s; in = new BufferedReader (new InputStreamReader (client.getInputStream (), "GB2312")); out = new PrintWriter (client.getOutputStream (), true); out.println ("--Welcome--"); start ();} public void run () {try {String line = in.readLine () While (! line.equals ("bye")) {String msg = createMessage (line); out.println (msg); line = in.readLine ();} out.println ("--See you, bye!--"); client.close ();} catch (IOException e) {}} private String createMessage (String line) {xxxxxxxxx;}} public static void main (String [] args) throws IOException {new Server () }}

This program listens on port 10000 and gives the access to the CreateServer thread to run. The CreateServerThread thread accepts the input and responds to the customer until the customer enters "bye" and the thread ends. In the createMessage method, we can process the input, produce the result, and then return the result to the customer.

After reading the above, do you have any further understanding of the design principle of deep dialysis Java Socket sharing? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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