Skip to content
Related Articles

Related Articles Class in Java

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 24 Mar, 2022

The is a socket option that is connected with a socket, as the set of channels packages is java.nio.channels.NetworkChannel that this interface has defined the setOption as well as the getOption methods to set and query’s the channels within its Socket Options.

--> Package
    --> SocketOption Class   

This class allows end-user to generate the socketOptions objects that are useful in invoking all fundamentals socket operations We can do networking operations such as sending, reading data and transferring, closing to connections, And Each of these SocketOption objects is created using with class has invoked perfectly with one remote host and for connecting to another level host, therefore, we won’t generate the new object for it.

Syntax: Importing SocketOption class from package


SocketOption class is invoking a stream SocketOption and which is connected to the simplified port number and port addressed as this SocketOption class is used for the generation of the socket and is connecting to the similar remote address on the accessible remote ports that are in to use.

Method Action Performed
name() Returns the name of the SocketOption to it.
type() Returns the type of the socket option value to it.

Uses of Interfaces- Class

Packages that Use the SocketOption Class

Package Description              This will feature the classes for invoking networking applications.
com.sun.nio.sctp                A Java API for invoking the Streaming Control Transporting Protocols
java.nio.channels                            This is defining the channels as well as connections to sets of channels that create performing I/O operations, like as files and sockets, and defines selectors and for multiplexed thus non-blocking I/O operations. This has Platform specified sockets options similar to the and java.nio.channels socketOption classes

Uses of SocketOption Class Of Package 

Modifier and Type Field Description
static final SocketOption<NetworkInterface>                                        StandardSocketOptions.IP_MULTICAST_IF                                            The networking interface for this Internet Protocol (IP) is in the multicasted datagrams
static final SocketOption<Boolean>                                  StandardSocketOptions.IP_MULTICAST_LOOP Loopbacking of Internet Protocol (IP) is in  multicasted datagrams
static final SocketOption<Integer> StandardSocketOptions.IP_MULTICAST_TTL                                            the time-to-live in SocketOption<Integer> is for the Internet Protocol (IP) is also in multicasted datagrams
static final SocketOption<Integer> StandardSocketOptions.IP_TOS The Type of Services in SocketOption<Integer> (ToS) octeting within Internet Protocol (IP) header
static final SocketOption<Boolean> StandardSocketOptions.SO_BROADCAST Giving permission to transmission of broadcasting datagrams
static final SocketOption<Boolean> StandardSocketOptions.SO_KEEPALIVE this will keeping the connection always alive
static final SocketOption<Integer> StandardSocketOptions.SO_LINGER it will Linger within on close if data is store in it
static final SocketOption<Integer> StandardSocketOptions.SO_RCVBUF this will received a buffer by size of its socket
static final SocketOption<Boolean> StandardSocketOptions.SO_REUSEADDR                        it is a reusable addresses
static final SocketOption<Boolean>                  StandardSocketOptions.SO_REUSEPORT it is a reusable port
static final SocketOption<Integer> StandardSocketOptions.SO_SNDBUF sending a buffer by size of sockets
static final SocketOption<Boolean> StandardSocketOptions.TCP_NODELAY it will terminating the Nagle algorithm

Uses of SocketOption within the java.nio.channels

Methods in java.nio.channels has returning types within the arguments of type with SocketOption

Modifier and Type Method and Description
Set<SocketOption<?>> Returning the sets of the socket having options are supported by its modifier type channels

Methods in java.nio.channels within the parameters of type with SocketOption 

Modifier and Type  Method and Description
<T> T

NetworkChannel.getOption(SocketOption<T> name)

throwing the value of SocketOption

abstract <T> DatagramChannel DatagramChannel.setOption(SocketOption<T> name, T value) 
abstract <T> SocketChannel SocketChannel.setOption(SocketOption<T> name, T value) 
abstract <T> AsynchronousServerSocketChannel AsynchronousServerSocketChannel.setOption(SocketOption<T> name, T value)
abstract <T> ServerSocketChannel ServerSocketChannel.setOption(SocketOption<T> name, T value) 
<T> NetworkChannel

NetworkChannel.setOption(SocketOption<T> name, T value)

this will setting up the values of SocketOption

abstract <T> AsynchronousSocketChannel AsynchronousSocketChannel.setOption(SocketOption<T> name, T value) 

Implementation: Java Examples for a SocketOption class Demonstration

A. For the Server-Side


// Java Program to Illustrate SocketOption Class
// Importing required classes
import *;
// Class
public class MyServers {
    // Main driver method
    public static void main(String[] args)
        // Try block to check for exceptions
        try {
            // Creating object of class inside main() method
            ServerSocket aa = new ServerSocket(6666);
            // Setting up an connection
            SocketOption sock = aa.accept();
            // Initializing an input stream
            DataInputStream disc
                = new DataInputStream(s.getInputStream());
            String str = (String)disc.readUTF();
            System.out.println("message= " + str);
            // Closing the socket
            // using close() method
        // Catch block to handle exceptions at runtime
        catch (Exception c) {
            // Display exception along with line number

Output: On the Client

B. For the Client-Side


// Java Program to Illustrate SocketOption Class
// Importing required classes
import *;
// Class
public class MyClients {
    // Main driver method
    public static void main(String[] args)
        // Try block to check for exceptions
        try {
            // Initializing the SocketOptions
            SocketOption soc
                = new SocketOption("localhost", 6666);
            DataOutputStream d = new DataOutputStream(
            // Display message only
            d.writeUTF("Hello GFG Readers!");
            // closing that enabled DataOutputStream
            // Closing the sockets
        // Invoking an Exception in running time
        // Catch block to handle exceptions
        catch (Exception e) {
            // Display message only

Output: On the server

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!