为什么 Java DatagramSocket return false on "isConnected" 肯定是连接的?

Why does Java DatagramSocket return false on "isConnected" when it is definitely connected?

我正在学习简单的 UDP 教程 HERE,但我 运行 遇到了问题。

//DSender.java  
import java.net.*;  
public class DSender{  
  public static void main(String[] args) throws Exception {  
    try{
        DatagramSocket ds = new DatagramSocket();  
        String str = "Welcome java";  
        InetAddress ip = InetAddress.getByName("127.0.0.1");  

        DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000);  
        ds.send(dp);  
        System.out.println(ds.isConnected());
    } catch(Exception e){
    } finally {
        ds.close();  
    }
  }  
} 


//DReceiver.java  
import java.net.*;  
public class DReceiver{  
  public static void main(String[] args) throws Exception {  
    try{
        DatagramSocket ds = new DatagramSocket(3000);  
        byte[] buf = new byte[1024];  
        DatagramPacket dp = new DatagramPacket(buf, 1024);  
        ds.receive(dp);  
        String str = new String(dp.getData(), 0, dp.getLength());  
        System.out.println(str);  
    } catch(Exception e){
    } finally {
        ds.close();  
    }
  }  
}  

在我关闭套接字之前,我执行了一个:

ds.send(dp);
System.out.println(ds.isConnected());
ds.close();

在连接上,它 总是 返回错误,即使它肯定已连接,并且已成功从客户端向服务器发送消息。阅读 Java 7 API,它说:

If the socket was connected prior to being closed, then this method will continue to return true after the socket is closed.

因为我在关闭前调用了 isConnected() 方法,所以它应该是 true。仅供参考,我也使用了 getPort() 方法,它总是 returns "-1",也表明它没有连接,即使它是。

If the socket was connected prior to being closed, then this method will continue to return the connected port number after the socket is closed.

这是怎么回事?

编辑: 我发布了我链接到的页面的完整代码。

要使 isConnected() 的输出为真,您需要首先使用方法 public void connect(InetAddress host, int port).

将 DatagramSocket 连接到特定的 InetAddress 和端口号

如果您没有将它连接到特定的 InetAddress 和端口,isConnected() 的结果将为 false。您可以在您的代码上进行测试。

来自 Managing Connections topic in Chapter 12. UDP, of Java Network Programming Fourth Edition :-

The connect() method doesn’t really establish a connection in the TCP sense. However, it does specify that the DatagramSocket will only send packets to and receive packets from the specified remote host on the specified remote port. Attempts to send packets to a different host or port will throw an IllegalArgumentException. Packets received from a different host or a different port will be discarded without an exception or other notification.

A security check is made when the connect() method is invoked. If the VM is allowed to send data to that host and port, the check passes silently. If not, a SecurityException is thrown. However, once the connection has been made, send() and receive() on that DatagramSocket no longer make the security checks they’d normally make.

同样,大约

public int getPort()

If and only if a DatagramSocket is connected, the getPort() method returns the remote port to which it is connected. Otherwise, it returns –1.

大约

public void disconnect()

The disconnect() method breaks the “connection” of a connected DatagramSocket so that it can once again send packets to and receive packets from any host and port.