Java中的getHostAddress()和getInetAddress() [英] getHostAddress() and getInetAddress() in Java

查看:165
本文介绍了Java中的getHostAddress()和getInetAddress()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建TCP套接字应用程序。在服务器端,

I am creating TCP socket application. In server side,

ss = new ServerSocket(10000);
Socket socket = ss.accept();
String remoteIp = socket.getInetAddress().getHostAddress();
String RemotePort = ":"+socket.getLocalPort();

我对最后两行有点困惑,getInetAddress()用于返回地址socket连接到,即是host的地址?然后我们为什么需要一个getHostAddress()?

I am a little bit confused about the last two lines, getInetAddress() used to return the address of socket connect to, namely is the address of host? and then why we need a getHostAddress()?

推荐答案

socket.getInetAddress()返回 InetAddress 包含远程计算机IP地址的对象。

socket.getInetAddress() returns an InetAddress object that contains the IP address of the remote machine.

InetAddress.getHostAddress()返回 String 对象以及该地址的文字表示。

InetAddress.getHostAddress() returns a String object with the textual representation of that address.

所以,最后得到一个字符串你可以打印,这是怎么回事你做到了。

So, to end up with a String you can print, that's how you do it.

编辑:如果您不熟悉,这称为'方法链接'。这与说法相同:

In case you're not familiar, this is called 'method chaining'. It's the same thing as saying:

InetAddress addy = socket.getInetAddress();
String remoteIp = addy.getHostAddress();

这篇关于Java中的getHostAddress()和getInetAddress()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆