0

I have a project in JAVA, in which i bind tcp sockets in specific ports.

When i call the close() method for a socket bind on port X, i cannot bind a new socket on port X for a few seconds, although i have called the close() method. But after a few seconds it is ok to bind.

Is this normal to be happening? Or should I check my code again?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
NickPro
  • 304
  • 3
  • 14

1 Answers1

3

I bind tcp sockets in specific ports

That's the problem right there, and the solution is not to do it. Let the system choose.

Binding to specific local ports isn't a solution to anything, at least not to anything sane. Occasionally overenthusiastic netadmins specify outbound port ranges in the mistaken belief or delusion that (a) it adds to security and (b) it is easily implementable. Neither is correct.

The TIME_WAIT state isn't 'the OS [taking] a while to be informed', it is an essential security feature of TCP. Closed sockets linger for a couple of minutes. Unless you are binding to specific local outbound ports this is a feature, not a bug.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • It's just that for the purpose of the project, my team has only 10 ports available to deploy its clients and server. I understand that it may not be right (as the system itself can bind these ports for its own processes), but i have no other choice than a bank of specific ports. – NickPro May 25 '12 at 06:12
  • @kit-kat Your system has as many outbound client ports as it likes unless someone has made a stupid rule as above. The only limitation should be on *listening* ports, i.e. ServerSocket ports. You don't have to specify a local outbound port to comply with that. If someone doesn't agree send them here for the argument. – user207421 May 25 '12 at 06:23
  • http://www.quickmeme.com/meme/3pfxjd/ . Totally agree with you. I discussed this with a colleague and he mentioned that the OS takes a while to be informed of the closing of a port, and thus make it available for binding. I guess there is no way to inform the OS manually. Or is there? – NickPro May 25 '12 at 08:43
  • @kit-kat If you are referring to the TIME_WAIT state, it isn't 'the OS [taking] a while to be informed', it is an essential security feature of TCP. Closed sockets linger for a couple of minutes. Unless you are binding to specific local outbound ports this is a feature, not a bug. There are thousands of ports that can be system-allocated as outbound ports: let the system allocate them. – user207421 May 25 '12 at 08:47
  • I searched about this state, and I now understand what is going on. As i mentioned before, this is a college project, so i was concerned about the exhibition of the project to my professor. But if it is a security feature of TCP, then there is nothing to concern about. Thanks a lot. – NickPro May 25 '12 at 09:05
  • I dont know if it would be a good idea to edit your answer, so it includes your comment about TIME_WAIT. It will be a very good answer. – NickPro May 25 '12 at 09:08