According to the documentation passing null to the Socket.bind() method will cause system to automatically assign an ephemeral port number to the socket.
Whenever I try to do it, I am getting "SocketException: Already Bound".
I tried reading into documentation, but being new to programming I don't fully understand everything that's there yet. Isn't Socket.bind(null) supposed to find an available port?
Also if I am being daft and following bad practices anywhere else in the code you are welcome to scorn me.
Below is the fragment of the code:
public static void main(String[] args) { ServerSocket ss = null; try { ss = new ServerSocket(8080); } catch (IOException e) { e.printStackTrace(); } System.out.println("Listening on port " + ss.getLocalPort()); Socket socket = null; try { socket = ss.accept(); socket.bind(null); //crashes here Thread connection = new Thread(new Echo(socket)); connection.run(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } https://stackoverflow.com/questions/66792044/java-socket-bindnull March 25, 2021 at 10:04AM
没有评论:
发表评论