此类实现IP套接字地址(IP地址和端口号的组合)。此类的对象是不可变的,可以用于绑定、连接目的。
施工人员:
1.InetSocketAddress(InetAddress addr,int-port): 此构造函数类似于套接字地址的一般结构,具有Inet地址和端口号的属性。
Syntax :public InetSocketAddress(InetAddress addr, int port)Parameters :addr : IP addressport : port number
2.InetSocketAddress(int端口): 创建具有指定端口号和通配符IP地址的socketaddress对象。通配符IP地址的值为0.0.0.0,它将您的套接字绑定到所有网卡。
Syntax : public InetSocketAddress(int port)Parameters :port : port number
3.InetSocketAddress(字符串主机名,int端口): 创建socketaddress对象并将其绑定到指定的端口和主机。主机名解析是为了找到用于绑定的IP地址,而不是主机名。如果解析返回null,地址将被标记为未解析。
Syntax : public InetSocketAddress(String hostname, int port)Parameters :hostname : host nameport : port number
方法:
1.createUnresolved(): 使用给定的主机和端口号创建套接字地址,其中不尝试解析主机名,并且该地址被标记为未解析。
Syntax :public static InetSocketAddress createUnresolved(String host, int port)Parameters :host : host nameport : port number
2.getPort(): 返回此套接字地址的端口号。
Syntax : public final int getPort()
3.getAddress(): 返回此套接字地址的IP地址。
Syntax : public final InetAddress getAddress()
4.getHostName(): 返回主机名,如果是使用IP地址创建的,则使用反向查找。
Syntax : public final String getHostName()
5.getHostString(): 如果使用用于创建的地址文字的主机名或字符串表示形式创建,则返回主机名。
Syntax : public final String getHostString()
6.未解决() 返回一个布尔值,指示是否解析此地址。
Syntax : public final boolean isUnresolved()
7.toString(): 返回此InetSocket地址对象的字符串表示形式。首先对InetAddress部分调用toString()方法,然后在冒号后面追加端口号。
Syntax : public String toString()
8.等于: 比较此socketaddress对象是否等于指定的对象。如果它们代表相同的inetaddress和端口号,或者如果地址未解析,则表示主机名和端口号,则两者相等。
Syntax : public final boolean equals(Object obj)Parameters :obj : object to compare with
9.hashcode(): 返回此InetSocketAddress对象的哈希代码。
Syntax : public final int hashCode()
Java实现:
JAVA
// Java program to illustrate various // InetSocketAddress class import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.UnknownHostException; public class InetsockAddress { public static void main(String[] args) throws UnknownHostException { // Following constructor can be used to create InetSocketAddress // objects. InetSocketAddress isa1 = new InetSocketAddress( 5500 ); InetSocketAddress isa2 = new InetSocketAddress( "localhost" , 5050 ); InetAddress ip = InetAddress.getByName( "localhost" ); InetSocketAddress isa3 = new InetSocketAddress(ip, 8800 ); // createUnresolved() does not attempt to resolve the hostname. InetSocketAddress isa4 = InetSocketAddress.createUnresolved( "abc" , 5055 ); // These InetSocketAddress objects can be used to create sockets // in socket programming, in place of specifying individually the IP // address and port number. Please refer to TCP articles for their // further use. // These can also be used to retrieve information about the // socketAddress objects. // getHostName() method System.out.println( "Hostname : " + isa1.getHostName()); // getHostString() method System.out.println( "Host string : " + isa1.getHostString()); // getAddress() method System.out.println( "Inet address : " + isa1.getAddress()); // getPort() method System.out.println( "Port : " + isa1.getPort()); // isUnresolved() method System.out.println( "isUnresolved : " + isa1.isUnresolved()); // equals() method System.out.println( "isa1==isa2 : " + isa1.equals(isa2)); // toString() method System.out.println( "toString : " + isa1.toString()); // hashCode() method System.out.println( "hashCode : " + isa1.hashCode()); } } |
输出:
Hostname : 0.0.0.0Host string : 0.0.0.0Inet address : 0.0.0.0/0.0.0.0Port : 5500isUnresolved : falseisa1==isa2 : falsetoString : 0.0.0.0/0.0.0.0:5500hashCode : 5500
参考资料: 官方Java文档 本文由 Rishabh Mahrsee .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。