Detailed explanation of the implementation process of ServerSocket default IP binding

Detailed explanation of the implementation process of ServerSocket default IP binding

When the server needs to be started during development, local testing directly writes the port, and the actual environment also requires specifying the IP to be bound.

Because sometimes the server has more than one network card, and our system must communicate through the specified IP and port, so the IP and port used by the server need to define the configuration file.

So in normal testing, when no IP is specified, where does ServerSocket bind to?

In this case, the server will bind this port to 0.0.0.0, that is, it will bind it to all IPs, and can receive requests on each IP. As for what 0.0.0.0 is, I won’t explain it here.

The following is a test program, through the following program you can see what the situation is:

package test;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Test {
	public static void main(String[] args) throws Exception {
		SocketClient client = new SocketClient();
		new Thread(client).start();
		
		ServerSocket serverSocket = new ServerSocket();
		serverSocket.bind(new InetSocketAddress(8888));
// serverSocket.bind(new InetSocketAddress("127.0.0.1",8888));
// serverSocket.bind(new InetSocketAddress("192.168.1.100",8888));
		System.out.println(serverSocket.toString());
		serverSocket.accept();
	}
}
class SocketClient implements Runnable{
	public void run() {
		try {
			Thread.sleep(2000);
			try {
				Socket socket = new Socket("127.0.0.1", 8888);
				System.out.println("127.0.0.1 success" + socket.toString());
			} catch (Exception e) {
				System.err.println("127.0.0.1 failed");
			}
			try {
				Socket socket = new Socket("192.168.1.100", 8888);
				System.out.println("192.168.1.100 success" + socket.toString());
			} catch (Exception e) {
				System.err.println("192.168.1.100 failed");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

The print result at this time is:

ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=8888]
127.0.0.1 Success Socket[addr=/127.0.0.1,port=8888,localport=59213]
192.168.1.100 Success Socket[addr=/192.168.1.100,port=8888,localport=59214]

Print results when using 127.0.0.1:

ServerSocket[addr=/127.0.0.1,port=0,localport=8888]
127.0.0.1 Success Socket[addr=/127.0.0.1,port=8888,localport=59416]
192.168.1.100 failed

Print results when using 192.168.1.100:

ServerSocket[addr=/192.168.1.100,port=0,localport=8888]
127.0.0.1 failed
192.168.1.100 Success Socket[addr=/192.168.1.100,port=8888,localport=59429]

You can see that the default binding is 0.0.0.0. At this time, you can use any IP that can be marked to this computer to access port 8888.

Of course, this is not a verification of dual network cards, because I am still playing on one network card. If conditions permit, you can do a small test on a blade machine.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Design Optimization Scheme of ServerSocket in Java Network Communication
  • Example explanation based on Socket class and ServerSocket class
  • JAVA-NIO Socket/ServerSocket Channel (Detailed Explanation)
  • [Java] Detailed explanation of Socket and ServerSocket study notes
  • Socket and ServerSocket class construction methods and API

<<:  Differences between MySQL CHAR and VARCHAR when storing and reading

>>:  Vue3 encapsulates its own paging component

Recommend

Implementation of single process control of Linux C background service program

introduce Usually a background server program mus...

Vue uses openlayers to load Tiandi Map and Amap

Table of contents 1. World Map 1. Install openlay...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

In-depth understanding of Vue's data responsiveness

Table of contents 1. ES syntax getter and setter ...

Detailed explanation of the process of building an MQTT server using Docker

1. Pull the image docker pull registry.cn-hangzho...

Ideas and codes for implementing Vuex data persistence

What is vuex vuex: is a state manager developed s...

Detailed explanation of transaction isolation levels in MySql study notes

background When we talk about transactions, every...

Nodejs combined with Socket.IO to realize websocket instant communication

Table of contents Why use websocket Socket.io Ope...

React Native startup process detailed analysis

Introduction: This article takes the sample proje...

Complete steps for using Echarts and sub-packaging in WeChat Mini Program

Preface Although the holiday is over, it shows up...

How to purchase and initially build a server

I haven't worked with servers for a while. No...

How to install and uninstall open-vswitch in Linux

1. Compile and install ovs from source code: Inst...

MySQL date functions and date conversion and formatting functions

MySQL is a free relational database with a huge u...

Docker Basic Tutorial: Detailed Explanation of Dockerfile Syntax

Preface Dockerfile is a script interpreted by the...