gpt4 book ai didi

java - 为什么 Java DatagramSocket 在确定连接时在 "isConnected"上返回 false?

转载 作者:行者123 更新时间:2023-11-29 03:02:52 25 4
gpt4 key购买 nike

我正在学习简单的 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。作为一个 FYI,我也使用了 getPort() 方法,它总是返回“-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() 的输出为真,您需要先将 DatagramSocket 连接到特定的 InetAddress 和端口号,使用方法 public void connect(InetAddress host, int port) .

如果您没有将它连接到特定的 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.

关于java - 为什么 Java DatagramSocket 在确定连接时在 "isConnected"上返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33744324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com