当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java java.net.ProtocolFamily用法及代码示例


ProtocolFamily 是一个 java 接口。 java.net.ProtocolFamily 代表一系列通信协议。 Java.net.StandardProtocolFamily 实现ProtocolFamily 接口。

public interface ProtocolFamily 

Methods of java.net.ProtocolFamily

java.net.ProtocolFamily 接口仅包含一种方法:

编号 方法 说明 返回类型
1. name() name()方法返回协议族的名称 String

Use of ProtocolFamily Interface

编号 修饰符和类型 姓名 说明
1. java.net class StandardProtocolFamily StandardProtocolFamily 类定义通信协议的标准系列。
2. java.nio.channels 静态DatagramChannel DatagramChannel.open(ProtocolFamily家族) DatagramChannel.open() 静态方法打开数据报通道。
3. java.nio.channels.spi 摘要DatagramChannel SelectorProvider.openDatagramChannel(ProtocolFamily家族) SelectorProvider.openDatagramChannel 打开数据报通道。

用于更好地理解 ProtocolFamily 接口的 Java 程序

Java


// Java Program to get Protocol 
// family for an IP Address 
  
// import Guava library 
import com.google.common.net.InetAddresses; 
import java.io.*; 
import java.net.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
        try { 
            // to get the ip address of 
            InetAddress ip 
                = InetAddress.getByName("45.22.30.39"); 
            System.out.println("Host Name is "
                               + ip.getHostName()); 
  
            // getProtocolFamily() will check and return the 
            // protocol family of the given IP 
            ProtocolFamily protocolFamily 
                = getProtocolFamily(ip); 
  
            System.out.println("Protocol family of the "
                               + ip.getHostName() + " is "
                               + protocolFamily); 
        } 
        catch (Exception e) { 
            System.out.println("Some exception"
                               + e.getMessage()); 
        } 
    } 
  
    public static ProtocolFamily 
    getProtocolFamily(InetAddress inetAddress) 
    { 
        // to check if address is valid or not 
        if (InetAddresses.isInetAddress( 
                inetAddress.getHostName())) { 
            // if address is valid 
            // will return the protocol family of the 
            // address 
            return StandardProtocolFamily.INET; 
        } 
        else { 
            // address is not valid 
            System.out.println( 
                "Address " + inetAddress 
                + "is invalid hence can't determine the protocol family"); 
        } 
        return StandardProtocolFamily.INET; 
    } 
}
输出
Host Name is 45.22.30.39
Protocol family of the 45.22.30.39 is INET


相关用法


注:本文由纯净天空筛选整理自harshsethi2000大神的英文原创作品 java.net.ProtocolFamily Class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。