java InetSocketAddress 与 InetAddress
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14648699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
InetSocketAddress vs InetAddress
提问by Rajat
Can anyone please explain me is there is any difference between InetAddress
and InetSocketAddress
classes in java, and if there is a difference, please explain the same. I google-ed it up but I couldn't find anything explanatory.
任何人都可以解释一下java中的InetAddress
和InetSocketAddress
类之间有什么区别,如果有区别,请解释相同。我用谷歌搜索了它,但找不到任何解释。
回答by Robin Chander
An instance of an InetAddress consists of an IP address and possibly its corresponding host name
InetAddress 的实例由 IP 地址和可能的对应主机名组成
This class implements an IP Socket Address (IP address + port number) It can also be a pair (hostname + port number), in which case an attempt will be made to resolve the hostname
这个类实现了一个IP Socket地址(IP地址+端口号),也可以是一对(主机名+端口号),在这种情况下会尝试解析主机名
回答by mike
They represent different OSI layers:
它们代表不同的OSI 层:
An
InetAddress
corresponds to the Network Layer (Layer 3)and is basically an IP address.A
InetSocketAddress
corresponds to the Transport Layer (Layer 4)and consists of an IP address and a port number.
An
InetAddress
对应于网络层(第 3 层),基本上是一个 IP 地址。A
InetSocketAddress
对应传输层(第 4 层),由 IP 地址和端口号组成。