• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in java.net.Bind Exception

 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

In my Networking programming, How to kill the process that using a port no.
Actually, I started my Server program which was using a port say 4000. Now once I again starts the server , It shows the


Since, the port no. is in use.
I want to programtically unbind the used port . So that once i restart the server file it will kill the running porcess using that port.

Thanks
Abhijit
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I surely hope nothing like that exists. For some ports it may not be that drastic, but what if I write an application that tries to bind on port 80? Boom! No more webserver. Port 135? Boom! File sharing is gone.

Instead you should close your application nicely, so it will unbind the port itself.

And by the way, port 4000 is reserved for something called Terabase. See IANA port numbers.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I want the application should able to stop the running port first if it is used and then start executing the program. That is, to avoid java.lang.BindException.


thanks
Abhijit
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhijit Das:
hi,

I want the application should able to stop the running port first if it is used and then start executing the program. That is, to avoid java.lang.BindException.


thanks
Abhijit


As he said, you won't need to if the application has shutdown and freed up the resources.
So ask first why it's still in use.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I just simply track the given port is using if not, create the socket with the port.else kill the port and then create the socket with the given port.

Thanks
Abhijit
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's _still_ bad. If you track down say port 80 and then destroy that, then you are destroying the default web server's ability to be connected to! What you need to do is have a snippet of code that tries to connect to a port that is free, so in other words have a loop that connects to a port, checks if it gets a Bind exception, if it does then try another port.
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Martijn,

I understood your logic. But, the same code will be used in different nodes in cluster of servers. So, I will assign a specific port in harcoded and all the server should able to create ServerSocket in that port only, it shuold not through any BindingException.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic