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:
Print results when using 127.0.0.1:
Print results when using 192.168.1.100:
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:
|
<<: Differences between MySQL CHAR and VARCHAR when storing and reading
>>: Vue3 encapsulates its own paging component
Table of contents Normal paging query How to opti...
Preface The role of process management: Determine...
Table of contents 1. Element offset series 2. Ele...
Mysql supports 3 types of lock structures Table-l...
1.watch listener Introducing watch import { ref, ...
As a required course for front-end developers, CS...
On the road to self-learning game development, th...
Table of contents The order in which MySQL reads ...
I have been working on a project recently - Budou...
Let's first look at several ways to submit a ...
1. Mycat application scenarios Mycat has been dev...
For example, when you create a new table or updat...
1. Mobile selection of form text input: In the te...
Mysql left join is invalid and how to use it When...
For websites with an architecture like LNMP, they...