Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-1261598

DatagramSocket.close blocks until a packet is received

    XMLWordPrintable

Details

    • 1.1
    • sparc
    • solaris_2.5
    • Not verified

    Description

      DatagramSocket.receive and DatagramSocket.close are both synchronized on the same monitor, so any thread attempting to close a socket blocks until a packet is received on the socket (if another thread is currently waiting for a packet). This is not right.

      This code demonstrates the problem:

      import java.net.*;
      import java.io.*;
      public class SocketClose
      implements Runnable
      {
      public static void main(String args[])
      {
      try {
      DatagramSocket sock = new DatagramSocket();
      System.out.println("Opened socket on port "+sock.getLocalPort());
      Thread t = new Thread(new SocketClose(sock));
      t.start();
      Thread.sleep(1000);
      System.out.println("Attempting to close...");
      sock.close();
      System.out.println("Closed!");
      }
      catch (Exception e)
      {
      System.err.println("Closer thread exception:");
      e.printStackTrace();
      }
      }
      DatagramSocket sock;
      public SocketClose(DatagramSocket sock)
      {
      this.sock = sock;
      }
      public void run()
      {
      try {sock.receive(new DatagramPacket(new byte[1000],1000));}
      catch (IOException e)
      {
      System.err.println("Receiver thread exception:");
      e.printStackTrace();
      }
      }
      }

      Attachments

        Activity

          People

            busersunw Btplusnull User (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: