How to get the remote port number in asp.net

X

xparrot1

I know that I can get the SERVER port number like this:

HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?

Thanks
Derek
 
T

tdavisjr

For communication to work, the client and server would have to use the same
port. So, it's safe to say the the client port and the server port should be
the same. However, this may not be the case. I'm sure someone would correct
me if I am wrong.
 
J

Juan T. Llibre

HttpContext.Current.Request.UserHostAddress
or Request.UserHostAddress if you're working inline code.
 
J

Juan T. Llibre

Please ignore that reply.

I thought you were asking for the user's IP address, not the port.

re:
My question is how do I get the remote CLIENT port number?

There's no such animal.
The client will make the request on whatever port the server is configured for.





Juan T. Llibre said:
HttpContext.Current.Request.UserHostAddress
or Request.UserHostAddress if you're working inline code.




I know that I can get the SERVER port number like this:

HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?

Thanks
Derek
 
M

Mark Rae

I know that I can get the SERVER port number like this:

HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?

I'm not sure which port you're referring to here...???
 
X

xparrot1

I'm referring to the client TCP/IP port number that the http response
will be sent on. If I go onto the web server and use a utility like
netstat I can see both sides of the connection -- usually, the server
port number will be 80 or 443 and the client will be a high port
number. I'm wondering if there is a way to access this port number via
code.



I know that I can get the SERVER port number like this:
HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?I'm not sure which port you're referring to here...???
 
T

tdavisjr

I think that high port number you may be seeing is a result of NAT that is
usually done by routers. You will not be able to get this port number since
this is not seen from the outside world. So, in the end, the client and
server will communicate using the same port (80 or 443); but NAT makes it
seem like its using another port.

I'm referring to the client TCP/IP port number that the http response
will be sent on. If I go onto the web server and use a utility like
netstat I can see both sides of the connection -- usually, the server
port number will be 80 or 443 and the client will be a high port
number. I'm wondering if there is a way to access this port number via
code.



I know that I can get the SERVER port number like this:
HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?I'm not sure
which port you're referring to here...???
[/QUOTE]
 
J

Juan T. Llibre

re:
I'm referring to the client TCP/IP port number that the http response will be sent on.
I'm wondering if there is a way to access this port number via code.

I think that's a random number above 1024.
It's picked from the pool of available ports (so as to avoid picking one that currently in use).

There's absolutely no programming need which could be
filled by accessing it before the packets are actually sent.





I'm referring to the client TCP/IP port number that the http response
will be sent on. If I go onto the web server and use a utility like
netstat I can see both sides of the connection -- usually, the server
port number will be 80 or 443 and the client will be a high port
number. I'm wondering if there is a way to access this port number via
code.



I know that I can get the SERVER port number like this:
HttpContext.Current.Request.ServerVariables["SERVER_PORT"]

My question is how do I get the remote CLIENT port number?I'm not sure which port you're
referring to here...???
[/QUOTE]
 
M

Mark Rae

I'm referring to the client TCP/IP port number that the http response
will be sent on. If I go onto the web server and use a utility like
netstat I can see both sides of the connection -- usually, the server
port number will be 80 or 443 and the client will be a high port
number. I'm wondering if there is a way to access this port number via
code.

Oh right...

Hmm - IIRC this is an entirely random port picked from a pool of available
ports between something like 1024 and 65536, but I don't think there's any
way of finding out its number through the standard HttpRequest /
HttpResponse connectivity.

AAMOI, what possible use could this number be to you...?
 
X

xparrot

Hmm - IIRC this is an entirely randomportpicked from a pool of available
ports between something like 1024 and 65536, but I don't think there's any
way of finding out its number through the standard HttpRequest /
HttpResponse connectivity.

AAMOI, what possible use could this number be to you...?

To answer your question -- The remote port can be used to determine the
machine that a request was originally routed from behind a NAT enabled
router. When NAT is used, all requests behind a router appear to come
from the same IP address. The only thing that differs is the port
number. NAT uses the port number to route the request back to the
proper machine on the private subnet.

In response to previous posts, the request would rarely be sent back to
the client on port 80 or 443. These ports would only be open on the
server machine. In fact, some personal firewalls would block these
ports entirely on a client PC. Usually the client initiates the
request and chooses a random high number port for its side of the
socket and connects to a known port on the server (80,443). When the
response is returned from the server it it sent back via the high
numbered client port for which it originated. You can observe this by
running "netstat -N" from the command line on a web server. It will
show all open client connections to the web server. You will see the
high numbered remote ports with the client address and standard ports
80 and 443 on the server side. Hope this clears up the port
confusion.
From the responses so far, it sounds like there is no easy way to
access this information via ASP.net.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top