用任何主机的IP地址构造InetSocketAddress的正确方法是什么? [英] What is the correct way to construct InetSocketAddress with any host an IP address?

查看:1119
本文介绍了用任何主机的IP地址构造InetSocketAddress的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个InetSocketAddress,但是无论我得到了host:port还是ip:port,我都想做对.我看到它有两个构造函数,一个用于主机(字符串),另一个用于IP(InetAddress).为了在这两个构造函数之间进行选择,是否必须确定自己是否拥有IP或主机?我在这里想念什么吗?

I want to create an InetSocketAddress but I want to do it right no matter if I get a host:port or a ip:port. I see it has two constructors, one for host (String) and another one for IP (InetAddress). Do I have to determine myself if I got an IP or HOST in order to choose between these two constructors? Am I missing something here?

推荐答案

您可以从Javadoc推断出,并在源代码中看到new InetSocketAddress(String hostname, int port)调用了InetAddress.getByName(hostname),该文档为您整理了所有内容.

You can infer from the Javadoc, and see in the source code, that new InetSocketAddress(String hostname, int port) calls InetAddress.getByName(hostname), which sorts all that out for you as documented.

因此,您发布的问题实际上并不存在.只需传递您得到的任何字符串,无论是主机名还是IP地址.

So the problem you're posting about doesn't really exist. Just pass whatever string you get, whether host name or IP address.

这篇关于用任何主机的IP地址构造InetSocketAddress的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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