简体   繁体   中英

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.

//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. Reading the Java 7 API, it says:

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

Since I called the isConnected() method before closing, it should read 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.

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) .

If you're not connecting it to a particular InetAddress and port, the result of isConnected() will be false. You could test than on your code.

From 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.

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. Otherwise, it returns –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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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