What is the purpose of an unbound socket?

I am new to networking in Java and in general. I read on sockets and saw that the ServerSocket class had a constructor for an unbound socket.

I thought that the operation of the sockets was connected to connecting to certain ports. How can I use an unrelated server server?

thanks.

+6
source share
2 answers

An ServerSocket cannot be used to accept connections before binding.

A no-argument constructor that does not yet bind ServerSocket exists, so you can call other methods in ServerSocket before binding it using the bind method. There, one method, in particular, must be called before binding: setReuseAddress(boolean on) . This Javadok mentions:

Enabling SO_REUSEADDR before binding the socket using {@link #bind (SocketAddress)} allows you to bind the socket, although the previous connection is in a timeout state.

(You can also say that the constructors that perform the binding are just convenient methods, so you don't have to call the bind(SocketAddress) method separately.)

+2
source

You need a port. You will need to call the bind method on the ServerSocket instance to bind the port to this instance.

+1
source

All Articles