gpt4 book ai didi

java - 连接到 Java 套接字时,为什么 getInetAddress() 返回/0 :0:0:0:0:0:0:1?

转载 作者:行者123 更新时间:2023-11-30 07:40:04 26 4
gpt4 key购买 nike

我通过 MS Telnet 连接,这是我看到输出的地方。

我知道 0:0:0:0:0:0:0:0 是 ALL 或通配符...但是它从哪里得出 0:0:0:0:0:0:0:1 呢?

给我这个有什么意义?为什么不给我一个IP比如127.0.0.1?

这是虚拟 MAC 地址吗? Javadoc 提供的信息不多。

public InetAddress getInetAddress() Returns the address to which the socket is connected. If the socket was connected prior to being closed, then this method will continue to return the connected address after the socket is closed.

Returns: the remote IP address to which this socket is connected, or null if the socket is not connected. From https://docs.oracle.com/javase/7/docs/api/java/net/Socket.html#getInetAddress()

主要内容:

public static void main(String[] args) throws IOException {
/*
* The actual work of the server socket is performed by an instance
* of the {@code SocketImpl} class.
*/

// Open a port to accept connections on 8080
ServerSocket srvrSocket = new ServerSocket(8080);

// Block until someone connects
while (true) {

Socket socket = srvrSocket.accept();

handle(socket);
}
}


private static void handle(Socket socket) throws IOException {

try(
socket;
InputStream is = socket.getInputStream();
OutputStream os = socket.getOutputStream();
) {

// Print welcome
DataUtil.writeStringAsInts(os, socket.getInetAddress().toString());
.
.
.

最佳答案

IPV6 对本地主机有 0:0:0:0:0:0:0:1 的表示,当我们尝试从本地连接服务器套接字时,我们可能会得到 0: 0:0:0:0:0:0:1 有时我们可能会得到 ::1

The localhost (loopback) address, 0:0:0:0:0:0:0:1, and the IPv6 unspecified address, 0:0:0:0:0:0:0:0, are reduced to ::1 and ::, respectively https://en.wikipedia.org/wiki/IPv6_address

关于java - 连接到 Java 套接字时,为什么 getInetAddress() 返回/0 :0:0:0:0:0:0:1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58330916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com