最近在看java的网络编程,想找一个对照手册,硬是找不到。最后兜兜转转还是找到了一个质量比较好的分享出来供学习使用。
手册内容如图,不仅仅有英文还有中文翻译。当翻译不准确无法理解时可以自己翻译英文理解。
分享链接:https://pan.baidu.com/s/147ECWjJiaSp5nNn5hx3V2w
提取码:qwer
下面的为该文档内容的复制可以忽略不看。为了绕过csdn的发文规则复制的。
Module java.base
软件包 java.net
Class DatagramSocket 译:类DatagramSocket
- java.lang.Object
-
- java.net.DatagramSocket
-
All Implemented Interfaces:
Closeable
,AutoCloseable
已知直接子类:
MulticastSocket
public class DatagramSocket extends Object implements Closeable
This class represents a socket for sending and receiving datagram packets.A datagram socket is the sending or receiving point for a packet delivery service. Each packet sent or received on a datagram socket is individually addressed and routed. Multiple packets sent from one machine to another may be routed differently, and may arrive in any order.
Where possible, a newly constructed
DatagramSocket
has theSO_BROADCAST
socket option enabled so as to allow the transmission of broadcast datagrams. In order to receive broadcast packets a DatagramSocket should be bound to the wildcard address. In some implementations, broadcast packets may also be received when a DatagramSocket is bound to a more specific address.Example:
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888));
Which is equivalent to:DatagramSocket s = new DatagramSocket(8888);
Both cases will create a DatagramSocket able to receive broadcasts on UDP port 8888.
译: 此类表示用于发送和接收数据报包的套接字。数据报套接字是分组传送服务的发送或接收点。 在数据报套接字上发送或接收的每个数据包都是单独寻址和路由的。 从一台机器发送到另一台机器的多个分组可以被不同地路由,并且可以以任何顺序到达。
在可能的情况下,新构建的
DatagramSocket
启用了SO_BROADCAST
套接字选项,以允许传输广播数据报。 为了接收广播数据包,应将DatagramSocket绑定到通配符地址。 在一些实现中,当DatagramSocket绑定到更具体的地址时,也可以接收广播分组。示例:
DatagramSocket s = new DatagramSocket(null); s.bind(new InetSocketAddress(8888));
相当于:DatagramSocket s = new DatagramSocket(8888);
这两种情况都将创建一个能够在UDP端口8888上接收广播的DatagramSocket。从以下版本开始:
1.0
另请参见:
DatagramPacket
,DatagramChannel
-
-
Constructor Summary 译:构造函数摘要
构造方法 Modifier Constructor Description
译: 描述DatagramSocket()
Constructs a datagram socket and binds it to any available port on the local host machine.
译:构造一个数据报套接字并将其绑定到本地主机上的任何可用端口。
DatagramSocket(int port)
Constructs a datagram socket and binds it to the specified port on the local host machine.
译:构造一个数据报套接字并将其绑定到本地主机上的指定端口。
DatagramSocket(int port, InetAddress laddr)
Creates a datagram socket, bound to the specified local address.
译:创建绑定到指定本地地址的数据报套接字。
protected
DatagramSocket(DatagramSocketImpl impl)
Creates an unbound datagram socket with the specified DatagramSocketImpl.
译:使用指定的DatagramSocketImpl创建未绑定的数据报套接字。
DatagramSocket(SocketAddress bindaddr)
Creates a datagram socket, bound to the specified local socket address.
译:创建绑定到指定本地套接字地址的数据报套接字。
-
Method Summary 译:方法摘要
所有方法 静态方法 接口方法 具体的方法 Modifier and Type 方法 Description
译: 描述void
bind(SocketAddress addr)
Binds this DatagramSocket to a specific address and port.
译:将此DatagramSocket绑定到特定的地址和端口。
void
close()
Closes this datagram socket.
译:关闭此数据报套接字。
void
connect(InetAddress address, int port)
Connects the socket to a remote address for this socket.
译:将套接字连接到此套接字的远程地址。
void
connect(SocketAddress addr)
Connects this socket to a remote socket address (IP address + port number).
译:将此套接字连接到远程套接字地址(IP地址+端口号)。
void
disconnect()
Disconnects the socket.
译:断开插座。
boolean
getBroadcast()
Tests if SO_BROADCAST is enabled.
译:测试是否启用了SO_BROADCAST。
DatagramChannel
getChannel()
Returns the unique
DatagramChannel
object associated with this datagram socket, if any.
译:返回与此数据报套接字关联的唯一
DatagramChannel
对象(如果有)。InetAddress
getInetAddress()
Returns the address to which this socket is connected.
译:返回此套接字连接的地址。
InetAddress
getLocalAddress()
Gets the local address to which the socket is bound.
译:获取套接字绑定的本地地址。
int
getLocalPort()
Returns the port number on the local host to which this socket is bound.
译:返回此套接字绑定到的本地主机上的端口号。
SocketAddress
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.
译:返回此套接字绑定的端点的地址。
<T> T
getOption(SocketOption<T> name)
Returns the value of a socket option.
译:返回套接字选项的值。
int
getPort()
Returns the port number to which this socket is connected.
译:返回此套接字连接的端口号。
int
getReceiveBufferSize()
Get value of the SO_RCVBUF option for this
DatagramSocket
, that is the buffer size used by the platform for input on thisDatagramSocket
.
译:获取此
DatagramSocket
的SO_RCVBUF选项的值,即该平台在此DatagramSocket
上输入的缓冲区大小。SocketAddress
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
null
if it is unconnected.
译:返回此套接字连接到的端点的地址,如果未连接则返回
null
。boolean
getReuseAddress()
Tests if SO_REUSEADDR is enabled.
译:测试是否启用了SO_REUSEADDR。
int
getSendBufferSize()
Get value of the SO_SNDBUF option for this
DatagramSocket
, that is the buffer size used by the platform for output on thisDatagramSocket
.
译:获取此
DatagramSocket
的SO_SNDBUF选项的值,即该平台在此DatagramSocket
上用于输出的缓冲区大小。int
getSoTimeout()
Retrieve setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).
译:检索SO_TIMEOUT的设置。 0返回意味着该选项被禁用(即无穷大的超时)。
int
getTrafficClass()
Gets traffic class or type-of-service in the IP datagram header for packets sent from this DatagramSocket.
译:获取从此DatagramSocket发送的数据包的IP数据报头中的流量类或服务类型。
boolean
isBound()
Returns the binding state of the socket.
译:返回套接字的绑定状态。
boolean
isClosed()
Returns whether the socket is closed or not.
译:返回套接字是否关闭。
boolean
isConnected()
Returns the connection state of the socket.
译:返回套接字的连接状态。
void
receive(DatagramPacket p)
Receives a datagram packet from this socket.
译:从此套接字接收数据报包。
void
send(DatagramPacket p)
Sends a datagram packet from this socket.
译:从此套接字发送数据报包。
void
setBroadcast(boolean on)
Enable/disable SO_BROADCAST.
译:启用/禁用SO_BROADCAST。
static void
setDatagramSocketImplFactory(DatagramSocketImplFactory fac)
Sets the datagram socket implementation factory for the application.
译:设置应用程序的数据报套接字实现工厂。
<T> DatagramSocket
setOption(SocketOption<T> name, T value)
Sets the value of a socket option.
译:设置套接字选项的值。
void
setReceiveBufferSize(int size)
Sets the SO_RCVBUF option to the specified value for this
DatagramSocket
.
译:将SO_RCVBUF选项设置为此
DatagramSocket
的指定值。void
setReuseAddress(boolean on)
Enable/disable the SO_REUSEADDR socket option.
译:启用/禁用SO_REUSEADDR套接字选项。
void
setSendBufferSize(int size)
Sets the SO_SNDBUF option to the specified value for this
DatagramSocket
.
译:将SO_SNDBUF选项设置为此
DatagramSocket
的指定值。void
setSoTimeout(int timeout)
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
译:使用指定的超时启用/禁用SO_TIMEOUT,以毫秒为单位。
void
setTrafficClass(int tc)
Sets traffic class or type-of-service octet in the IP datagram header for datagrams sent from this DatagramSocket.
译:在IP数据报头中为从此DatagramSocket发送的数据报设置流量类或服务类型八位字节。
Set<SocketOption<?>>
supportedOptions()
Returns a set of the socket options supported by this socket.
译:返回此套接字支持的一组套接字选项。
-
Methods declared in class java.lang.Object 译:在类java.lang中声明的方法。 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
-
-
Constructor Detail 译:构造函数详细信息
-
DatagramSocket 译:DatagramSocket的
public DatagramSocket() throws SocketException
Constructs a datagram socket and binds it to any available port on the local host machine. The socket will be bound to thewildcard
address, an IP address chosen by the kernel.If there is a security manager, its
checkListen
method is first called with 0 as its argument to ensure the operation is allowed. This could result in a SecurityException.
译: 构造一个数据报套接字并将其绑定到本地主机上的任何可用端口。 套接字将绑定到wildcard
地址,即内核选择的IP地址。如果有安全管理器,则首先调用其
checkListen
方法,并将0作为其参数,以确保允许操作。 这可能会导致SecurityException。异常
SocketException
- if the socket could not be opened, or the socket could not bind to the specified local port.
译:SocketException
- 如果无法打开套接字,或者套接字无法绑定到指定的本地端口。SecurityException
- if a security manager exists and itscheckListen
method doesn't allow the operation.
译:SecurityException
- 如果存在安全管理器且其checkListen
方法不允许该操作。另请参见:
SecurityManager.checkListen(int)
-
DatagramSocket 译:DatagramSocket的
protected DatagramSocket(DatagramSocketImpl impl)
Creates an unbound datagram socket with the specified DatagramSocketImpl.
译: 使用指定的DatagramSocketImpl创建未绑定的数据报套接字。参数
impl
- an instance of a DatagramSocketImpl the subclass wishes to use on the DatagramSocket.
译:impl
-一个 的DatagramSocketImpl的子类希望在DatagramSocket的使用的一个实例。从以下版本开始:
1.4
-
DatagramSocket 译:DatagramSocket的
public DatagramSocket(SocketAddress bindaddr) throws SocketException
Creates a datagram socket, bound to the specified local socket address.If, if the address is
null
, creates an unbound socket.If there is a security manager, its
checkListen
method is first called with the port from the socket address as its argument to ensure the operation is allowed. This could result in a SecurityException.
译: 创建绑定到指定本地套接字地址的数据报套接字。如果地址为
null
,则创建一个未绑定的套接字。如果有安全管理器,则首先使用套接字地址中的端口作为其参数调用其
checkListen
方法,以确保允许操作。 这可能会导致SecurityException。参数
bindaddr
- local socket address to bind, ornull
for an unbound socket.
译:bindaddr
- 要绑定的本地套接字地址,或未绑定套接字的null
。异常
SocketException
- if the socket could not be opened, or the socket could not bind to the specified local port.
译:SocketException
- 如果无法打开套接字,或者套接字无法绑定到指定的本地端口。SecurityException
- if a security manager exists and itscheckListen
method doesn't allow the operation.
译:SecurityException
- 如果存在安全管理器且其checkListen
方法不允许该操作。从以下版本开始:
1.4
另请参见:
SecurityManager.checkListen(int)
-
DatagramSocket 译:DatagramSocket的
public DatagramSocket(int port) throws SocketException
Constructs a datagram socket and binds it to the specified port on the local host machine. The socket will be bound to thewildcard
address, an IP address chosen by the kernel.If there is a security manager, its
checkListen
method is first called with theport
argument as its argument to ensure the operation is allowed. This could result in a SecurityException.
译: 构造一个数据报套接字并将其绑定到本地主机上的指定端口。 套接字将绑定到wildcard
地址,即内核选择的IP地址。如果存在安全管理器,则首先使用
port
参数作为其参数调用其checkListen
方法,以确保允许该操作。 这可能会导致SecurityException。参数
port
- port to use.
译:port
- 要使用的端口。异常
SocketException
- if the socket could not be opened, or the socket could not bind to the specified local port.
译:SocketException
- 如果无法打开套接字,或者套接字无法绑定到指定的本地端口。SecurityException
- if a security manager exists and itscheckListen
method doesn't allow the operation.
译:SecurityException
- 如果存在安全管理器且其checkListen
方法不允许该操作。另请参见:
SecurityManager.checkListen(int)
-
-