📜  Java中的Java .net.SocketOption 类

📅  最后修改于: 2022-05-13 01:55:36.052000             🧑  作者: Mango

Java中的Java .net.SocketOption 类

Java.net.SocketOption是一个与套接字连接的套接字选项,因为通道包的集合是Java.nio.channels.NetworkChannel ,该接口定义了 setOption 以及 getOption 方法来设置和查询通道在其套接字选项中。

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

这个Java.net.SocketOption 类允许最终用户生成用于调用所有基本套接字操作的 socketOptions 对象我们可以进行网络操作,例如发送、读取数据和传输、关闭连接,这些 SocketOption 对象中的每一个都是使用Java.net.SocketOption 创建的类已经完美地调用了一个远程主机并连接到另一个级别的主机,因此,我们不会为它生成新对象。

语法:从Java.net 包中导入 SocketOption 类

import java.net.SocketOption;

SocketOption 类正在调用流 SocketOption 并连接到简化的端口号和寻址的端口,因为此 SocketOption 类用于生成套接字并连接到 javax.net 中可访问远程端口上的类似远程地址使用。

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

接口的使用 - Java.net.SocketOption 类

使用SocketOption类的

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

Java.net包的SocketOption类的使用 

Modifier and TypeFieldDescription
static final SocketOption                                       StandardSocketOptions.IP_MULTICAST_IF                                           The networking interface for this Internet Protocol (IP) is in the multicasted datagrams
static final SocketOption                                 StandardSocketOptions.IP_MULTICAST_LOOPLoopbacking of Internet Protocol (IP) is in  multicasted datagrams
static final SocketOptionStandardSocketOptions.IP_MULTICAST_TTL                                           the time-to-live in SocketOption is for the Internet Protocol (IP) is also in multicasted datagrams
static final SocketOptionStandardSocketOptions.IP_TOSThe Type of Services in SocketOption (ToS) octeting within Internet Protocol (IP) header
static final SocketOptionStandardSocketOptions.SO_BROADCASTGiving permission to transmission of broadcasting datagrams
static final SocketOptionStandardSocketOptions.SO_KEEPALIVEthis will keeping the connection always alive
static final SocketOptionStandardSocketOptions.SO_LINGERit will Linger within on close if data is store in it
static final SocketOptionStandardSocketOptions.SO_RCVBUFthis will received a buffer by size of its socket
static final SocketOptionStandardSocketOptions.SO_REUSEADDR                       it is a reusable addresses
static final SocketOption                 StandardSocketOptions.SO_REUSEPORTit is a reusable port
static final SocketOptionStandardSocketOptions.SO_SNDBUFsending a buffer by size of sockets
static final SocketOptionStandardSocketOptions.TCP_NODELAYit will terminating the Nagle algorithm

Java.nio.channels 中 SocketOption 的使用

Java.nio.channels 中的方法在 SocketOption 类型的参数中具有返回类型

Modifier and TypeMethod and Description
Set>Returning the sets of the socket having options are supported by its modifier type channels

Java.nio.channels 中的方法内的参数类型为 SocketOption

Modifier and Type Method and Description
T

NetworkChannel.getOption(SocketOption name)

throwing the value of SocketOption

abstract DatagramChannelDatagramChannel.setOption(SocketOption name, T value) 
abstract SocketChannelSocketChannel.setOption(SocketOption name, T value) 
abstract AsynchronousServerSocketChannelAsynchronousServerSocketChannel.setOption(SocketOption name, T value)
abstract ServerSocketChannelServerSocketChannel.setOption(SocketOption name, T value) 
NetworkChannel

NetworkChannel.setOption(SocketOption name, T value)

this will setting up the values of SocketOption

abstract AsynchronousSocketChannelAsynchronousSocketChannel.setOption(SocketOption name, T value) 

实现: SocketOption 类演示的Java示例

A. 对于服务器端

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


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


输出:在客户端

B. 对于客户端

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

输出:在服务器上