📜  Java Java类

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

Java Java类

数据报套接字是一种网络套接字,它为发送和接收数据包提供无连接点。从数据报套接字发送的每个数据包都单独路由和传递。它也可以用于发送和接收广播消息。数据报套接字是 java 通过 UDP 而不是 TCP 提供网络通信的机制。

构造函数:

1. DatagramSocket() :创建一个datagramSocket并将其绑定到本地机器上的任何可用端口。如果使用此构造函数,则操作系统会将任何端口分配给此套接字。

Syntax :public DatagramSocket()
               throws SocketException
Throws :
SocketException : if the socket could not be opened

2. DatagramSocket(DatagramSocketImpl impl):用给定的datagramImpl创建一个未绑定的数据报套接字。

Syntax :protected DatagramSocket(DatagramSocketImpl impl)
Parameters :
impl : instance of datagramScketImpl

3. DatagramSocket(int port):创建数据报套接字并将其绑定到指定端口。套接字将绑定到内核选择的通配符地址。

Syntax : public DatagramSocket(int port)
               throws SocketException
Parameters : 
port : port number to bind this socket to

4. DatagramSocket(int port, InetAddress laddr) :构造一个数据报套接字并将其绑定到指定的端口和inetaddress。

Syntax : public DatagramSocket(int port,
              InetAddress laddr)
               throws SocketException
Parameters :
port : local port to bind
laddr : local address to bind
Throws :
SocketException : If the socket could not be opened

5. DatagramSocket(SocketAddress bindaddr):构造一个新的socket对象,并绑定到指定的socket地址(IP地址+端口号)。

Syntax :public DatagramSocket(SocketAddress bindaddr)
               throws SocketException
Parameters :
bindaddr : socket address to bind to
Throws : 
SocketException : If socket could not be opened

方法 :

1. bind() :将此套接字绑定到指定的地址和端口号。

Syntax : public void bind(SocketAddress addr)
Parameters : 
addr : socket address to bind to

2. connect() :连接到指定的地址和端口。连接到远程主机后,此套接字只能发送或接收来自该远程主机的数据包。如果无法与指定的远程主机建立连接,则对 send() 或 receive() 的调用将抛出 PortUnreachable Exception。

Syntax :public void connect(InetAddress address,
           int port)
Parameters :
address : address of remote host
port : port number of remote host

另一个重载方法将套接字地址作为参数。

Syntax :public void connect(SocketAddress address)
Parameters :
address : socket address of remote host

3. disconnect() :断开套接字。如果套接字未连接,则此方法无效。

Syntax :public void disconnect()

4. isBound() :返回一个布尔值,指示此套接字是否已绑定。

Syntax :public boolean isBound()

已连接() : 返回一个布尔值,指示此套接字是否已连接。

Syntax :public boolean isConnected()

5. isConnected() :返回代表套接字连接状态的布尔值。请注意,即使在关闭套接字之后,如果此套接字在关闭套接字之前已连接,则此方法将继续返回 true。

Syntax :public boolean isConnected()

6. getInetAddress() :返回此套接字连接的地址。

Syntax : public InetAddress getInetAddress()

7. getPort() :返回此套接字连接到的机器上的端口。

Syntax : public int getPort()

8. getRemoteSocketAddress() :返回这个socket连接的socket地址(IP地址+端口号)。

Syntax : public SocketAddress getRemoteSocketAddress()

9. getLocalSocketAddress() :返回此socket绑定的机器地址,即本机socket地址。

public SocketAddress getLocalSocketAddress()

10. send() :从这个套接字发送一个数据报包。需要注意的是,关于要发送的数据的信息、发送到的地址等都是由数据包本身处理的。

Syntax : public void send(DatagramPacket p)
Parameters :
p : packet to send

11. receive() :用于接收来自发送者的数据包。当成功接收到一个数据包时,数据包的缓冲区将被接收到的消息填充。该数据包还包含有价值的信息,例如发件人地址和端口号。此方法一直等到收到数据包。

Syntax : public void receive(DatagramPacket p)
Parameters : 
p : datagram packet into which incoming data is filled

12. getLocalAddress() :返回此套接字绑定的本地地址。

Syntax : public InetAddress getLocalAddress()

13. getLocalPort() :返回绑定此套接字的本地机器上的端口。

Syntax : public int getLocalPort()

14. setSOTimeout() :用于设置接收数据报包的等待时间。由于对 receive() 方法的调用会无限期地阻止程序的执行,直到接收到数据包,所以此方法可用于限制该时间。一旦指定的时间到期,就会抛出Java.net.SocketTimeoutException。

Syntax : public void setSoTimeout(int timeout)
Parameters :
timeout : time to wait

15. getSoTimeout() :如果指定,则返回超时参数,或 0 表示无限时间。

Syntax : public int getSoTimeout()

16. setSendBufferSize() :用于设置可以从该套接字发送的数据包的最大大小限制。它设置 SO_SNDBUF 选项,网络实现使用该选项设置底层网络缓冲区的大小。当发送速率很高时,增加大小可能允许在发送之前对数据包进行排队。

Syntax : public void setSendBufferSize(int size)
Parameters : 
size : size of send buffer to set

Java实现:

Java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Arrays;
   
public class datasocket 
{
    public static void main(String[] args) throws IOException 
    {
        // Constructor to create a datagram socket
        DatagramSocket socket = new DatagramSocket();
        InetAddress address = InetAddress.getByName("localhost");
        int port = 5252;
        byte buf[] = { 12, 13 };
        byte buf1[] = new byte[2];
        DatagramPacket dp = new DatagramPacket(buf, 2, address, port);
        DatagramPacket dptorec = new DatagramPacket(buf1, 2);
           
        // connect() method
        socket.connect(address, port);
       
        // isBound() method
        System.out.println("IsBound : " + socket.isBound());
   
        // isConnected() method
        System.out.println("isConnected : " + socket.isConnected());
   
        // getInetAddress() method
        System.out.println("InetAddress : " + socket.getInetAddress());
   
        // getPort() method
        System.out.println("Port : " + socket.getPort());
   
        // getRemoteSocketAddress() method
        System.out.println("Remote socket address : " + 
                         socket.getRemoteSocketAddress());
   
        // getLocalSocketAddress() method
        System.out.println("Local socket address : " + 
                          socket.getLocalSocketAddress());
   
        // send() method
        socket.send(dp);
        System.out.println("...packet sent successfully....");
   
        // receive() method
        socket.receive(dptorec);
        System.out.println("Received packet data : " + 
                          Arrays.toString(dptorec.getData()));
   
        // getLocalPort() method
        System.out.println("Local Port : " + socket.getLocalPort());
   
        // getLocalAddress() method
        System.out.println("Local Address : " + socket.getLocalAddress());
   
        // setSOTimeout() method
        socket.setSoTimeout(50);
   
        // getSOTimeout() method
        System.out.println("SO Timeout : " + socket.getSoTimeout());
    }
   
}


Java
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class smallserver {
   
    public static void main(String[] args) throws IOException {
           
        DatagramSocket ds = new DatagramSocket(5252);
        byte buf[] = new byte[2];
        byte send[] = { 13, 18 };
        DatagramPacket dp = new DatagramPacket(buf, 2);
   
        ds.receive(dp);
   
        DatagramPacket senddp = new DatagramPacket(send, 2, 
                               dp.getAddress(), dp.getPort());
        ds.send(senddp);
    }
   
}


Java
import java.io.IOException;
import java.net.DatagramSocket;
   
public class datasock2 {
   
    public static void main(String[] args) throws IOException {
   
        // Constructor
        DatagramSocket socket = new DatagramSocket(1235);
   
        // setSendBufferSize() method
        socket.setSendBufferSize(20);
   
        // getSendBufferSize() method
        System.out.println("Send buffer size : " + 
                         socket.getSendBufferSize());
   
        // setReceiveBufferSize() method
        socket.setReceiveBufferSize(20);
   
        // getReceiveBufferSize() method
        System.out.println("Receive buffer size : " + 
                           socket.getReceiveBufferSize());
   
        // setReuseAddress() method
        socket.setReuseAddress(true);
   
        // getReuseAddress() method
        System.out.println("SetReuse address : " + 
                             socket.getReuseAddress());
   
        // setBroadcast() method
        socket.setBroadcast(false);
   
        // getBroadcast() method
        System.out.println("setBroadcast : " + 
                              socket.getBroadcast());
   
        // setTrafficClass() method
        socket.setTrafficClass(45);
   
        // getTrafficClass() method
        System.out.println("Traffic class : " + 
                           socket.getTrafficClass());
   
        // getChannel() method
        System.out.println("Channel : " + 
       ((socket.getChannel()!=null)?socket.getChannel():"null"));
   
        // setSocketImplFactory() method
        socket.setDatagramSocketImplFactory(null);
   
        // close() method
        socket.close();
   
        // isClosed() method
        System.out.println("Is Closed : " + socket.isClosed());
   
    }
}


为了测试上述程序,需要一个小的服务器程序来接收发送的数据包并实现receive()方法。下面给出它的实现。

Java

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class smallserver {
   
    public static void main(String[] args) throws IOException {
           
        DatagramSocket ds = new DatagramSocket(5252);
        byte buf[] = new byte[2];
        byte send[] = { 13, 18 };
        DatagramPacket dp = new DatagramPacket(buf, 2);
   
        ds.receive(dp);
   
        DatagramPacket senddp = new DatagramPacket(send, 2, 
                               dp.getAddress(), dp.getPort());
        ds.send(senddp);
    }
   
}

输出:在客户端

IsBound : true
isConnected : true
InetAddress : localhost/127.0.0.1
Port : 5252
Remote socket address : localhost/127.0.0.1:5252
Local socket address : /127.0.0.1:59498
packet sent successfully
Received packet data : [13, 18]
Local Port : 59498
Local Address : /127.0.0.1
SO Timeout : 50

17. getSendBufferSize() :返回此套接字的 SO_SNDBUF 选项的值。

Syntax : public int getSendBufferSize()

18. setReceiveBufferSize() :用于设置在此套接字上接收的数据包的最大大小限制。它设置 SO_RCVBUF 选项,网络实现使用该选项设置底层网络缓冲区的大小。当数据包的发送速度快于它们的消耗速度时,增加大小可能允许在接收端对数据包进行排队。

Syntax : public void setReceiveBufferSize(int size)
Parameters : 
size : size of receive buffer to set

19. getReceiveBufferSize() :返回此套接字的 SO_RCVBUF 选项的值。

Syntax : public int getReceiveBufferSize()

20. setReuseAddress() :有时可能需要将多个套接字绑定到同一个地址。启用此选项可使其他套接字绑定到与此相同的地址。它必须在调用 bind() 之前设置。它设置 SO_REUSEADDR 套接字选项的值。

Syntax : public void setReuseAddress(boolean on)
Parameters : 
on : true for enable, false otherwise

21. getReuseAddress() :返回布尔值,表示 SO_REUSEADDR 套接字选项的设置。

Syntax : public boolean getReuseAddress()

22. setBroadcast() :设置 SO_BROADCAST 套接字选项的值。

Syntax : public void setBroadcast(boolean on)
Parameters : 
on : true to allow broadcast, false otherwise

23. getBroadcast() :如果启用广播,则返回 true,否则返回 false。

Syntax : public boolean getBroadcast()

24. setTrafficClass() :用于设置从这个DatagramSocket发送的数据报的IP数据报头中的服务类型八位字节。有关流量的更多详细信息,请参阅 Wikipedia

Syntax : public void setTrafficClass(int tc)
Parameters : 
tc : int value of bitset, 0<=tc<=255

25. getTrafficClass() :从这个套接字发送的数据包的IP头中获取流量等级或服务类型。

Syntax : public int getTrafficClass()

26. close() :关闭这个数据报套接字。任何挂起的接收调用都会抛出 SocketException。

Syntax : public void close()

27. isClosed() :返回布尔值,指示套接字是否关闭。

Syntax : public boolean isClosed()

28. getChannel() :如果有任何与此套接字关联的数据通道,则返回一个数据通道。有关数据报通道的更多详细信息可以在Java官方文档中找到。

Syntax : public DatagramChannel getChannel()

29. setDatagramSocketImplFactory() :为应用程序设置数据报套接字实现工厂。

Syntax :public static void setDatagramSocketImplFactory(
                                 DatagramSocketImplFactory fac)
Parameters : 
fac - the desired factory.
Throws : 
IOException - if an I/O error occurs when setting the datagram socket factory.
SocketException - if the factory is already defined.

Java实现:

Java

import java.io.IOException;
import java.net.DatagramSocket;
   
public class datasock2 {
   
    public static void main(String[] args) throws IOException {
   
        // Constructor
        DatagramSocket socket = new DatagramSocket(1235);
   
        // setSendBufferSize() method
        socket.setSendBufferSize(20);
   
        // getSendBufferSize() method
        System.out.println("Send buffer size : " + 
                         socket.getSendBufferSize());
   
        // setReceiveBufferSize() method
        socket.setReceiveBufferSize(20);
   
        // getReceiveBufferSize() method
        System.out.println("Receive buffer size : " + 
                           socket.getReceiveBufferSize());
   
        // setReuseAddress() method
        socket.setReuseAddress(true);
   
        // getReuseAddress() method
        System.out.println("SetReuse address : " + 
                             socket.getReuseAddress());
   
        // setBroadcast() method
        socket.setBroadcast(false);
   
        // getBroadcast() method
        System.out.println("setBroadcast : " + 
                              socket.getBroadcast());
   
        // setTrafficClass() method
        socket.setTrafficClass(45);
   
        // getTrafficClass() method
        System.out.println("Traffic class : " + 
                           socket.getTrafficClass());
   
        // getChannel() method
        System.out.println("Channel : " + 
       ((socket.getChannel()!=null)?socket.getChannel():"null"));
   
        // setSocketImplFactory() method
        socket.setDatagramSocketImplFactory(null);
   
        // close() method
        socket.close();
   
        // isClosed() method
        System.out.println("Is Closed : " + socket.isClosed());
   
    }
}

输出 :

Send buffer size : 20
Receive buffer size : 20
SetReuse address : true
setBroadcast : false
Traffic class : 44
Channel : null
Is Closed : true

参考:官方Java文档