Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Limiting ServerSocket to a range of ports  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a client/server app that opens two sockets between the client and server. The server app is pretty typical, it opens a ServerSocket on a specific port. When the client connects it spawns a client handler thread to communicate with the new client. The first thing the client handler thread does is create a ServerSocket to allow the client to create an additional socket connection to the server.

Here is the problem. I'd like to restrict the range of ports so that I can program my router to forward that specific range of ports to the server.

Now, if I call new ServerSocket(0) I'll get an available port but not necessarily in the range of ports that I want.

Is there some way to find an available port or better, specify the range of ports to the ServerSocket constructor?

Thanks,
Brad
 
author and iconoclast
Posts: 24203
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brad,

Welcome to JavaRanch!

A decidedly low-tech answer: just try ports until you find one:

 
Brad Thomas
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick response. I wondered if there was a more "direct" way than that.

Brad
 
I like tacos! And this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!