search
HomeJavajavaTutorialDetailed explanation of the principles of how Java implements native socket communication mechanism

This article mainly introduces the principle of implementing the native socket communication mechanism in JAVA. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

This article introduces the principle of implementing native socket communication mechanism in JAVA and shares it with everyone. The details are as follows:

Current environment

jdk == 1.8

Knowledge points

  • Socket connection processing

  • IO input and output stream processing

  • Request data format processing

  • Request model optimization

Scenario

Today, let’s talk about socket communication issues in JAVA. Here we take the simplest one-request-one-response model as an example, assuming that we now need to communicate with the Baidu site. How can we use JAVA's native socket to achieve this?

Establish a socket connection

First, we need to establish a socket connection (core code)


import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
    
// 初始化 socket
Socket socket = new Socket();
// 初始化远程连接地址
SocketAddress remote = new InetSocketAddress(host, port);
// 建立连接
socket.connect(remote);

Processing socket input and output streams

After successfully establishing a socket connection, we can obtain its input and output streams. The essence of communication is the processing of input and output streams. Through the input stream, the data from the network connection is read, and through the output stream, the local data is sent to the remote end.

Socket connection is actually somewhat similar to processing file streams, both are performing IO operations.

The code to obtain the input and output streams is as follows:


// 输入流
InputStream in = socket.getInputStream();
// 输出流
OutputStream out = socket.getOutputStream();

Regarding the processing of IO streams, we generally use corresponding packaging classes to process IO streams. If If we deal with it directly, we need to operate on byte[], which is relatively cumbersome. If we use a wrapper class, we can directly process it with types such as string and int, which simplifies IO byte operations.

The following uses BufferedReader and PrintWriter as input and output packaging classes for processing.


// 获取 socket 输入流
private BufferedReader getReader(Socket socket) throws IOException {
  InputStream in = socket.getInputStream();
  return new BufferedReader(new InputStreamReader(in));
}

// 获取 socket 输出流
private PrintWriter getWriter(Socket socket) throws IOException {
  OutputStream out = socket.getOutputStream();
  return new PrintWriter(new OutputStreamWriter(out));
}

Data request and response

With the socket connection and IO input and output stream, the next step is to send Request data and get the response results of the request.

Because of the support of the IO packaging class, we can directly transmit in string format, and the packaging class will help us convert the data into the corresponding byte stream.

Because we are accessing the baidu site through HTTP, we do not need to define additional output formats. Using the standard HTTP transmission format, you can perform request responses (some specific RPC frameworks may have customized communication formats).

The requested data content is processed as follows:


public class HttpUtil {

  public static String compositeRequest(String host){

    return "GET / HTTP/1.1\r\n" +
        "Host: " + host + "\r\n" +
        "User-Agent: curl/7.43.0\r\n" +
        "Accept: */*\r\n\r\n";
  }
  
}

The code for sending the request data is as follows:


// 发起请求
PrintWriter writer = getWriter(socket);
writer.write(HttpUtil.compositeRequest(host));
writer.flush();
接收响应数据代码如下:

// 读取响应
String msg;
BufferedReader reader = getReader(socket);
while ((msg = reader.readLine()) != null){
  System.out.println(msg);
}

So far, we have finished talking about all the core codes for creating connections, sending requests and receiving responses under native sockets.

The complete code is as follows:


import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import com.test.network.util.HttpUtil;

public class SocketHttpClient {

  public void start(String host, int port) {

    // 初始化 socket
    Socket socket = new Socket();

    try {
      // 设置 socket 连接
      SocketAddress remote = new InetSocketAddress(host, port);
      socket.setSoTimeout(5000);
      socket.connect(remote);

      // 发起请求
      PrintWriter writer = getWriter(socket);
      System.out.println(HttpUtil.compositeRequest(host));
      writer.write(HttpUtil.compositeRequest(host));
      writer.flush();

      // 读取响应
      String msg;
      BufferedReader reader = getReader(socket);
      while ((msg = reader.readLine()) != null){
        System.out.println(msg);
      }

    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

  }

  private BufferedReader getReader(Socket socket) throws IOException {
    InputStream in = socket.getInputStream();
    return new BufferedReader(new InputStreamReader(in));
  }

  private PrintWriter getWriter(Socket socket) throws IOException {
    OutputStream out = socket.getOutputStream();
    return new PrintWriter(new OutputStreamWriter(out));
  }

}

Below, we show the results of socket communication by instantiating a client.


public class Application {

  public static void main(String[] args) {

    new SocketHttpClient().start("www.baidu.com", 80);

  }
}

Result output:

Request model optimization

In this way, there is no problem in implementing the function. But if we look closely, we find that IO blocking occurs during the IO writing and reading process. That is:


// 会发生 IO 阻塞
writer.write(HttpUtil.compositeRequest(host));
reader.readLine();

So if you want to request 10 different sites at the same time, as follows:


public class SingleThreadApplication {

  public static void main(String[] args) {

    // HttpConstant.HOSTS 为 站点集合
    for (String host: HttpConstant.HOSTS) {

      new SocketHttpClient().start(host, HttpConstant.PORT);

    }

  }
}

It must be After the first request response is completed, the next site processing will be initiated.

This is more obvious on the server side. Although the code here is a client connection, the specific operations are similar to those on the server side. Requests can only be processed serially one by one, which will definitely not meet the response time standard.

  • Multi-threading

Some people think this is not a problem at all. JAVA is a multi-threaded programming language. For this situation, a multi-threaded model is most appropriate.


public class MultiThreadApplication {

  public static void main(String[] args) {

    for (final String host: HttpConstant.HOSTS) {

      Thread t = new Thread(new Runnable() {
        public void run() {
          new SocketHttpClient().start(host, HttpConstant.PORT);
        }
      });

      t.start();

    }
  }
}

This method seems useful at first, but if the amount of concurrency is large, the application will use a lot of threads. As we all know, on the server, each thread actually occupies a file handle. The number of handles on the server is limited, and a large number of threads will cause considerable consumption of switching between threads. Therefore, this method must be unbearable in scenarios with large concurrency.

  • Multi-threading + thread pool processing

Since too many threads are not enough, then we can just control the number of threads created. Only a fixed number of threads are started for socket processing, which not only utilizes multi-threaded processing, but also controls system resource consumption.


public class ThreadPoolApplication {

  public static void main(String[] args) {

    ExecutorService executorService = Executors.newFixedThreadPool(8);

    for (final String host: HttpConstant.HOSTS) {

      Thread t = new Thread(new Runnable() {
        public void run() {
          new SocketHttpClient().start(host, HttpConstant.PORT);
        }
      });

      executorService.submit(t);
      new SocketHttpClient().start(host, HttpConstant.PORT);

    }

  }
}

Regarding the number of started threads, generally the CPU-intensive type will be set at N+1 (N is the number of CPU cores), and the IO-intensive type will be set at 2N + 1.

This method seems to be the optimal one. Is there anything better? If a thread can handle multiple socket connections at the same time and does not block when the input and output data of each socket is not ready, is it better? This technology is called "IO multiplexing". The corresponding implementation is provided in JAVA's nio package.

The above is the detailed content of Detailed explanation of the principles of how Java implements native socket communication mechanism. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use