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

How to quickly query 10 million records in Mysql

Table of contents Normal paging query How to opti...

8 commands to effectively manage processes in Linux

Preface The role of process management: Determine...

JavaScript implements three common web effects (offset, client, scroll series)

Table of contents 1. Element offset series 2. Ele...

Detailed explanation of the lock structure in MySQL

Mysql supports 3 types of lock structures Table-l...

The use and difference between vue3 watch and watchEffect

1.watch listener Introducing watch import { ref, ...

Achieve 3D flip effect with pure CSS3 in a few simple steps

As a required course for front-end developers, CS...

Detailed explanation on how to deploy H5 games to nginx server

On the road to self-learning game development, th...

Details of the order in which MySQL reads my.cnf

Table of contents The order in which MySQL reads ...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

Introduction to MyCat, the database middleware

1. Mycat application scenarios Mycat has been dev...

How to view mysql binlog (binary log)

For example, when you create a new table or updat...

Five practical tips for web form design

1. Mobile selection of form text input: In the te...

How to set up URL link in Nginx server

For websites with an architecture like LNMP, they...