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.
Asked
Active
Viewed 401 times
1 Answers
2
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()
).

Mordechai
- 15,437
- 2
- 41
- 82

John Bollinger
- 160,171
- 8
- 81
- 157
-
No, and once again no. You can set a [`ServerSocket`](http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#ServerSocket()) to listen to all available ports, by using the default constructor, or using port #0. – Mordechai Mar 16 '15 at 19:58
-
@MouseEvent, nope. The default constructor creates an *unbound* socket. If you pass port `0` to one of the other constructors, (or if you later bind your socket, specifying a port number of `0`) then you get a socket bound to a single non-zero port that is chosen for you by the system. Perhaps you are thinking of the behavior of specifying the all-zero address to bind to (one port on) each of the machine's addresses. – John Bollinger Mar 16 '15 at 20:03
-
Well, it's hard to apoligize, but you're right. Make a minor edit on your post so SO will let me take back my downvote (and of-course upvote). – Mordechai Mar 16 '15 at 21:09