Saturday, November 15, 2025
HomeLanguagesJavajava.net.ProtocolFamily Class in Java

java.net.ProtocolFamily Class in Java

ProtocolFamily is a java Interface. The java.net.ProtocolFamily represents a family of communication protocols. Java.net.StandardProtocolFamily implements ProtocolFamily Interface.

public interface ProtocolFamily 

Methods of java.net.ProtocolFamily

java.net.ProtocolFamily interface contains only one method:

S.No.         Method                                          Description                                                                    Return Type                                      
1. name() name() method returns the name of the protocol family String

Use of ProtocolFamily Interface

S.No. Package       Modifier and Type Name                                                                                               Description
1. java.net class StandardProtocolFamily StandardProtocolFamily class defines the standard families of communication protocols.
2. java.nio.channels static DatagramChannel  DatagramChannel.open(ProtocolFamily family) DatagramChannel.open() static method opens a datagram channel.
3. java.nio.channels.spi abstract DatagramChannel SelectorProvider.openDatagramChannel( ProtocolFamily family) SelectorProvider.openDatagramChannel Opens a datagram channel.

Java Program for Better Understanding of the ProtocolFamily Interface

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;
    }
}


Output

Host Name is 45.22.30.39
Protocol family of the 45.22.30.39 is INET
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32401 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6768 POSTS0 COMMENTS
Nicole Veronica
11919 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11990 POSTS0 COMMENTS
Shaida Kate Naidoo
6897 POSTS0 COMMENTS
Ted Musemwa
7149 POSTS0 COMMENTS
Thapelo Manthata
6850 POSTS0 COMMENTS
Umr Jansen
6841 POSTS0 COMMENTS