Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
java.net.URLConnection Class in Java
Next article icon

java.net.SocketOption Class in Java

Last Updated : 24 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The java.net.SocketOption 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.

--> java.net Package     --> SocketOption Class   

This java.net.SocketOption 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 java.net.SocketOption 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 java.net package

import java.net.SocketOption;

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 javax.net to use.

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

Uses of Interfaces- java.net.SocketOption Class

Packages that Use the SocketOption Class

PackageDescription
java.net             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.
jdk.netThis jdk.net has Platform specified sockets options similar to the java.net and java.nio.channels socketOption classes

Uses of SocketOption Class Of java.net Package 

Modifier and TypeFieldDescription
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_LOOPLoopbacking 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_TOSThe Type of Services in SocketOption<Integer> (ToS) octeting within Internet Protocol (IP) header
static final SocketOption<Boolean>StandardSocketOptions.SO_BROADCASTGiving permission to transmission of broadcasting datagrams
static final SocketOption<Boolean>StandardSocketOptions.SO_KEEPALIVEthis will keeping the connection always alive
static final SocketOption<Integer>StandardSocketOptions.SO_LINGERit will Linger within on close if data is store in it
static final SocketOption<Integer>StandardSocketOptions.SO_RCVBUFthis 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_REUSEPORTit is a reusable port
static final SocketOption<Integer>StandardSocketOptions.SO_SNDBUFsending a buffer by size of sockets
static final SocketOption<Boolean>StandardSocketOptions.TCP_NODELAYit 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 TypeMethod 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> DatagramChannelDatagramChannel.setOption(SocketOption<T> name, T value) 
abstract <T> SocketChannelSocketChannel.setOption(SocketOption<T> name, T value) 
abstract <T> AsynchronousServerSocketChannelAsynchronousServerSocketChannel.setOption(SocketOption<T> name, T value)
abstract <T> ServerSocketChannelServerSocketChannel.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> AsynchronousSocketChannelAsynchronousSocketChannel.setOption(SocketOption<T> name, T value) 

Implementation: Java Examples for a SocketOption class Demonstration

A. For the Server-Side

Java
// Java Program to Illustrate SocketOption Class  // Importing required classes import java.io.*; import java.net.*; import java.net.SocketOption *;  // 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             ss.close();         }          // Catch block to handle exceptions at runtime         catch (Exception c) {              // Display exception along with line number             System.out.println(c);         }     } } 

Output: On the Client

B. For the Client-Side

Java
// Java Program to Illustrate SocketOption Class  // Importing required classes import java.io.*; import java.net.*; import java.net.SocketOption *;  // 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(                 soc.getOutputStream());              // Display message only             d.writeUTF("Hello GFG Readers!");              d.flush();             // closing that enabled DataOutputStream             d.close();             // Closing the sockets             soc.close();         }          // Invoking an Exception in running time          // Catch block to handle exceptions         catch (Exception e) {              // Display message only             System.out.println(e);         }     } } 

Output: On the server


Next Article
java.net.URLConnection Class in Java

K

khurpaderushi143
Improve
Article Tags :
  • Java
  • Geeks Premier League
  • Geeks-Premier-League-2022
  • Java-Classes
  • Java-net-package
Practice Tags :
  • Java

Similar Reads

  • java.net.Socket Class in Java
    The java.net.Socket class allows us to create socket objects that help us in implementing all fundamental socket operations. We can perform various networking operations such as sending, reading data and closing connections. Each Socket object that has been created using with java.net.Socket class h
    5 min read
  • java.net.SocketPermission Class in Java
    The java.net.SocketPermisson class represents whether you have permission to access a network via sockets. A SocketPermission consists of a host and a set of actions. Class Declaration: public final class SocketPermission extends Permission implements SerializableConstructor: ConstructorDescriptionM
    2 min read
  • java.net.SocketImplFactory Class in Java
    In Java, SocketImplFactory Class is an interface java.net.SocketImplFactory Class is defining a factory for SocketImpl instances, as this interface is usable by sockets classes to create the sockets execution that implements various policies through it. Interface java.net.SocketImplFactory Class is
    2 min read
  • java.net.URLConnection Class in Java
    URLConnection Class in Java is an abstract class that represents a connection of a resource as specified by the corresponding URL. It is imported by the java.net package. The URLConnection class is utilized for serving two different yet related purposes, Firstly it provides control on interaction wi
    5 min read
  • java.net.NetPermission Class in Java
    NetPermission class is used to allow network permissions. NetPermission class extends BasicPermission class. It is a “named” permission i.e it contains a name but no action. Permission nameWhat permission allowsRisks associated with this permissionallowHttpTraceThis permission allows using the HTTP
    5 min read
  • java.net.Proxy Class in Java
    A proxy is an immutable object and type of tool or application or program or system, which helps to protect the information of its users and computers. It acts as a barrier between computer and internet users. A Proxy Object defines the Proxy settings to be used with a connection. Proxy servers are
    3 min read
  • java.net.SocketException in Java with Examples
    SocketException is a subclass of IOException so it's a checked exception. It is the most general exception that signals a problem when trying to open or access a socket. The full exception hierarchy of this error is: java.lang.Object java.lang.Throwable java.lang.Exception java.io.IOException java.n
    5 min read
  • 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
    2 min read
  • java.net.URL Class in Java
    URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part
    4 min read
  • Java.lang.Runtime class in Java
    In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences