Address reuse not working on new Java Runtime Environment

前端 未结 1 1651
一个人的身影
一个人的身影 2020-12-21 20:06

I am using the following Code for Checking Address Resusability:-

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket         


        
相关标签:
1条回答
  • 2020-12-21 20:27

    The javadocs for setReuseAddress say:

    When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed (typically known as the TIME_WAIT state or 2MSL wait state). For applications using a well known socket address or port it may not be possible to bind a socket to the required SocketAddress if there is a connection in the timeout state involving the socket address or port.

    Enabling SO_REUSEADDR prior to binding the socket using bind(SocketAddress) allows the socket to be bound even though a previous connection is in a timeout state.

    But what you are doing does not match this use-case. You are not attempting to bind while some other socket is closing. You are actually trying to bind while another socket is open.


    What puzzles me is why your test actually worked on older JREs. It might be a JVM bug ...

    0 讨论(0)
提交回复
热议问题