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

mysql method to recursively search for all child nodes of a menu node

background There is a requirement in the project ...

element-ui Mark the coordinate points after uploading the picture

What is element-ui element-ui is a desktop compon...

Solve the conflict between docker and vmware

1. Docker startup problem: Problem Solved: You ne...

Detailed explanation of MySQL cumulative calculation implementation method

Table of contents Preface Demand Analysis Mysql u...

A complete list of common Linux system commands for beginners

Learning Linux commands is the biggest obstacle f...

Use of vuex namespace

Table of contents Since Vuex uses a single state ...

Example of how to build a Mysql cluster with docker

Docker basic instructions: Update Packages yum -y...

HTML is actually the application of learning several important tags

After the article "This Will Be a Revolution&...

JavaScript implements asynchronous acquisition of form data

This article example shares the specific code for...

IIS and APACHE implement HTTP redirection to HTTPS

IIS7 Download the HTTP Rewrite module from Micros...

JavaScript canvas to achieve scratch lottery example

This article shares the specific code of JavaScri...

Linux firewall iptables detailed introduction, configuration method and case

1.1 Introduction to iptables firewall Netfilter/I...

Uniapp implements DingTalk scan code login sample code

Since Uniapp does not have DingTalk authorization...