Christie
Christie

Reputation: 1

Block socket connection for one port in JAVA

So basically I want my serversocket.accept() to be implemented for any other ports except 1218. Can I block the call from this port because 1218 has to connect to another thread.

Upvotes: 0

Views: 426

Answers (1)

John Bollinger
John Bollinger

Reputation: 181008

I have good news and bad news. The good news is that you don't have to do anything special to avoid listening on a particular port. The bad news is that's because any given ServerSocket listens for connections on only one port.

The port is how clients identify which of possibly many services they want to connect to. Some services listen for connections on multiple ports, but they need a separate socket for each one. It doesn't make sense for a server to listen on every port (nor either on all but one).

All you need to do is specify a port number different from 1218 when you bind your ServerSocket (whether via a constructor or via a separate bind()).

Upvotes: 2

Related Questions