src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java
changeset 58518 705c3f88a409
parent 55375 96c7427456f9
child 58679 9c3209ff7550
child 58808 9261ad32cba9
equal deleted inserted replaced
58517:252e7f4c4d92 58518:705c3f88a409
   873                 synchronized (stateLock) {
   873                 synchronized (stateLock) {
   874                     ensureOpen();
   874                     ensureOpen();
   875                     if (state == ST_CONNECTED)
   875                     if (state == ST_CONNECTED)
   876                         throw new AlreadyConnectedException();
   876                         throw new AlreadyConnectedException();
   877 
   877 
       
   878                     // ensure that the socket is bound
       
   879                     if (localAddress == null) {
       
   880                         bindInternal(null);
       
   881                     }
       
   882 
   878                     int n = Net.connect(family,
   883                     int n = Net.connect(family,
   879                                         fd,
   884                                         fd,
   880                                         isa.getAddress(),
   885                                         isa.getAddress(),
   881                                         isa.getPort());
   886                                         isa.getPort());
   882                     if (n <= 0)
   887                     if (n <= 0)
   930 
   935 
   931                     // no longer connected
   936                     // no longer connected
   932                     remoteAddress = null;
   937                     remoteAddress = null;
   933                     state = ST_UNCONNECTED;
   938                     state = ST_UNCONNECTED;
   934 
   939 
   935                     // refresh local address
   940                     // check whether rebind is needed
   936                     localAddress = Net.localAddress(fd);
   941                     InetSocketAddress isa = Net.localAddress(fd);
       
   942                     if (isa.getPort() == 0) {
       
   943                         // On Linux, if bound to ephemeral port,
       
   944                         // disconnect does not preserve that port.
       
   945                         // In this case, try to rebind to the previous port.
       
   946                         int port = localAddress.getPort();
       
   947                         localAddress = isa; // in case Net.bind fails
       
   948                         Net.bind(family, fd, isa.getAddress(), port);
       
   949                         isa = Net.localAddress(fd); // refresh address
       
   950                         assert isa.getPort() == port;
       
   951                     }
       
   952 
       
   953                     // refresh localAddress
       
   954                     localAddress = isa;
   937                 }
   955                 }
   938             } finally {
   956             } finally {
   939                 writeLock.unlock();
   957                 writeLock.unlock();
   940             }
   958             }
   941         } finally {
   959         } finally {