简体   繁体   English

当肯定连接时,为什么Java DatagramSocket在“ isConnected”上返回false?

[英]Why does Java DatagramSocket return false on “isConnected” when it is definitely connected?

I am following the simple UDP tutorial HERE , but I am running into an issue. 我在这里遵循简单的UDP教程,但是遇到了问题。

//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();  
    }
  }  
}  

Before I close the socket, I perform a: 在关闭插槽之前,请执行以下操作:

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

on the connection, and it always comes back false, even though it is definitely connected, and has successfully sent a message from the client to the server. 在连接上,即使它确实已连接,并且总是成功地将消息从客户端发送到服务器,但它总是返回false。 Reading the Java 7 API, it says: 阅读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. 如果套接字在关闭之前已连接,则此方法在套接字关闭后将继续返回true。

Since I called the isConnected() method before closing, it should read true. 由于我在关闭之前调用了isConnected()方法,因此它应显示为true。 As an FYI, I have also used the getPort() method, and it always returns "-1", also indicating that it is not connected, even though it is. 作为一个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. 如果套接字在关闭之前已连接,则此方法将在套接字关闭后继续返回连接的端口号。

What is going on? 到底是怎么回事?

EDIT: I posted the complete code from the page I linked to. 编辑:我张贴了我链接到的页面的完整代码。

To get the output of isConnected() true, you need to connect the DatagramSocket first to a particular InetAddress, and a port number, using the method public void connect(InetAddress host, int port) . 要使isConnected()的输出为true,您需要使用public void connect(InetAddress host, int port)方法将DatagramSocket首先连接到特定的InetAddress和端口号。

If you're not connecting it to a particular InetAddress and port, the result of isConnected() will be false. 如果未将其连接到特定的InetAddress和端口,则isConnected()的结果将为false。 You could test than on your code. 您可以对代码进行测试。

From Managing Connections topic in Chapter 12. UDP, of Java Network Programming Fourth Edition :- 摘自Java网络编程第四版第12章UDP中的“管理连接”主题

The connect() method doesn't really establish a connection in the TCP sense. connect()方法实际上并没有在TCP方面建立连接。 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. 但是,它确实指定了DatagramSocket将仅在指定的远程端口上向指定的远程主机发送数据包和从其接收数据包。 Attempts to send packets to a different host or port will throw an IllegalArgumentException . 尝试将数据包发送到其他主机或端口将抛出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. 调用connect()方法时,将进行安全检查。 If the VM is allowed to send data to that host and port, the check passes silently. 如果允许VM将数据发送到该主机和端口,则检查将以静默方式通过。 If not, a SecurityException is thrown. 如果不是,则抛出SecurityException However, once the connection has been made, send() and receive() on that DatagramSocket no longer make the security checks they'd normally make. 但是,一旦建立连接,该DatagramSocket上的send()和receive()就不再进行通常要进行的安全检查。

Similarly, about 同样,关于

public int getPort()

If and only if a DatagramSocket is connected, the getPort() method returns the remote port to which it is connected. 当且仅当连接了DatagramSocket时,getPort()方法才会返回其连接到的远程端口。 Otherwise, it returns –1. 否则,它返回–1。

and about 和关于

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. Disconnect()方法断开连接的DatagramSocket的“连接”,以便它可以再次向任何主机和端口发送数据包并从任何主机和端口接收数据包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM