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 optimize ServerSocket in Java Network Communication

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

Share

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

This article mainly introduces how to optimize ServerSocket in Java network communications, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

In the network communication of java, the hosts of two different nodes want to communicate by establishing a Socket object (equivalent to the client host, requesting to send information to the server) and a ServerSocket object (equivalent to the server host, receiving the connection request from the client and replying the message).

1: create a Socket object

Socket socket = new Socket ("IP", port)

Specify the ip address and port number of the server to be connected to create a Socket object, which can be exported and imported after creation.

2: create a ServerSocket object

ServerSocket sever = new ServerSocket (port); / / this port is the port connected by the client Socket connnection = server.accept (); / / the port above this monitor is blocked and processed as soon as there is a connection request.

Create a new ServerSocket object and have it block the specified port that listens to the client connection and process it as soon as there is a new connection request.

A basic relationship is: Socket-> ServerSocket

At this time, as long as there is a connection server, it will block and deal with it, and wait until the end of the processing to re-monitor the port status, if there is a new connection request. Keep going back and forth. The obvious disadvantage is inefficiency, only one request can be processed at a time, and the blocking processing state.

Optimization processing:

Multithreading is opened in BIO, and one thread is opened for each request server. The advantage is that it can improve efficiency, but the disadvantage is also obvious: a large number of advantages of almost simultaneous inbound connections can cause it to generate a large number of threads. In the end, the java virtual machine consumes memory and crashes.

Make improvements to the above scenario: thread pools are used in BIO to manage threads. It can be processed efficiently while preventing the server from crashing due to highly concurrent connections.

Use NIO. Although NIO is also a single thread to process connection requests, it does not block and constantly polls whether there are ready events, so the order of processing is independent of the order in which connection requests are made, but related to the order in which the request data arrives. You can call the multiplexing interface (select) in a thread to block and listen for IO requests from multiple clients at the same time, and call the corresponding function once an IO request is received.)

Thank you for reading this article carefully. I hope the article "how to optimize ServerSocket in Java Network Communication" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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