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 中可访问远程端口上的类似远程地址使用。Method Action 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类的包
Package | Description |
---|---|
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.net | This jdk.net has Platform specified sockets options similar to the java.net and java.nio.channels socketOption classes |
Java.net包的SocketOption类的使用
Modifier and Type | Field | Description |
---|---|---|
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_LOOP | Loopbacking of Internet Protocol (IP) is in multicasted datagrams |
static final SocketOption | StandardSocketOptions.IP_MULTICAST_TTL | the time-to-live in SocketOption |
static final SocketOption | StandardSocketOptions.IP_TOS | The Type of Services in SocketOption |
static final SocketOption | StandardSocketOptions.SO_BROADCAST | Giving permission to transmission of broadcasting datagrams |
static final SocketOption | StandardSocketOptions.SO_KEEPALIVE | this will keeping the connection always alive |
static final SocketOption | StandardSocketOptions.SO_LINGER | it will Linger within on close if data is store in it |
static final SocketOption | StandardSocketOptions.SO_RCVBUF | this will received a buffer by size of its socket |
static final SocketOption | StandardSocketOptions.SO_REUSEADDR | it is a reusable addresses |
static final SocketOption | StandardSocketOptions.SO_REUSEPORT | it is a reusable port |
static final SocketOption | StandardSocketOptions.SO_SNDBUF | sending a buffer by size of sockets |
static final SocketOption | StandardSocketOptions.TCP_NODELAY | it will terminating the Nagle algorithm |
Java.nio.channels 中 SocketOption 的使用
Java.nio.channels 中的方法在 SocketOption 类型的参数中具有返回类型
Modifier and Type | Method and Description |
---|---|
Set | Returning the sets of the socket having options are supported by its modifier type channels |
Java.nio.channels 中的方法内的参数类型为 SocketOption NetworkChannel.getOption(SocketOption throwing the value of SocketOption NetworkChannel.setOption(SocketOption this will setting up the values of SocketOptionModifier and Type Method and Description abstract DatagramChannel.setOption(SocketOption abstract SocketChannel.setOption(SocketOption abstract AsynchronousServerSocketChannel.setOption(SocketOption abstract ServerSocketChannel.setOption(SocketOption abstract AsynchronousSocketChannel.setOption(SocketOption
实现: 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);
}
}
}
输出:在服务器上