當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。