Re: Default Port Number for a particular "Scheme"?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Kevin Frey wrote:
Is there a .NET class where I can do something like this:

int defaultPort = SomeDotNetClass.GetDefaultPort( Uri.UriSchemeHttps );

No. However, the following will approximate it:

int defaultPort = new Uri(Uri.UriSchemeHttps + "://dummy").Port;

Although this approach produces invalid URIs (for example, "mailto://dummy"; isn't a valid URI as the mailto: scheme doesn't allow hierarchies and the "/" characters would need to be escaped to be part of a mailbox name), all the built-in parsers will accept this format as they all support hierarchies.

The Uri class is able to advise default port numbers for various schemes (but does not seem extensible)

You can derive a new UriParser and use UriParser.Register() to associate a default port with a new scheme. There is no way to simply associate a scheme with a port number without providing parsing capabilities, but there doesn't seem to be much use for that either. You could easily write your own class for this, though.

but I don't know whether they have been hard-coded or whether they have
been pulled from somewhere else, and I would dearly like to avoid
hard-coding 80, 443 etc in my application.

I wouldn't fret over this. The default ports are codified in RFCs. If you compare the effort required to make them changeable to the probability that they'll change within your application's lifetime it's obvious that it doesn't matter if you hard-code them.

The solution I presented above is kludgy in that it might break down if you introduce custom parsers that don't allow hierarchies at all, so it has little advantage over simply putting in a

const int HttpDefaultPort = 80;

where needed. In this particular case, it's more important that people know what the magic number 80 means than that it's easy to change. The least redundant solution that's still guaranteed not to break is to write your own specific class to associate schemes with port numbers, but this will be divorced from the framework's own mapping.

--
J.
.



Relevant Pages

  • Re: Behavior of flock() oriented procedures
    ... a port does not explicitly have a unique file descriptor ... generic port on some Scheme implementations, ... I am just wondering how you manage to obtain a lock if you don't ...
    (comp.unix.programmer)
  • Re: Ray tracer
    ... Jon Harrop writes: ... > enough to port my ray tracer from one of its other languages (C++, Java, ... > SML and OCaml) to Scheme: ... > let ray_sphere orig dir center radius = ...
    (comp.lang.scheme)
  • Re: SBS ISA. NAT vs. Watchguard
    ... That is just fine to use a private IP scheme. ... forward port 443 and 4125 for RWW. ... The SBS box will then run ISA, DHCP, DNS... ... If I do get a Watchguard that requires licenses and I have 50 users, ...
    (microsoft.public.security)
  • Re: SBS ISA. NAT vs. Watchguard
    ... That is just fine to use a private IP scheme. ... forward port 443 and 4125 for RWW. ... The SBS box will then run ISA, DHCP, DNS... ... If I do get a Watchguard that requires licenses and I have 50 users, ...
    (microsoft.public.backoffice.smallbiz)
  • Re: Resiliency To New Data Requirements
    ... more general than the hierarchal method? ... scheme; relations allow as many as are necessary... ... hierarchies have only the single way of indicating ... data with one particular data model vs another (ie ...
    (comp.databases.theory)