views:

425

answers:

4

I have a web service that is running on IIS (6 or 7, doesn't matter) and I would like to know the port that the caller has sent their request/invocation from.

So if a client makes a call on my web service, how do I find out from the server side what the port number is they made the call from?

Is that something that even gets passed at even the lowest level? Just to be clear I'm not looking for the port for callback purposes. It's for logging only.

A: 

If you service is on WCF, then:

OperationContext context = OperationContext.Current;

MessageProperties messageProperties = context.IncomingMessageProperties;

var endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] 
                       as RemoteEndpointMessageProperty;
if (endpointProperty != null)
{
    string sRemoteAddress = endpointProperty.Address;
    int nRemotePort = endpointProperty.Port;
}
Shrike
Looking for a solution for services that are not on WCF as well.
Leeks and Leaks
A: 

TCP sockets do have the concept of sender port number but it doesn't have much use in application level protocols. That said, considering the last paragraph of the OP, I think you're looking for some way to call back the client. The ports I said previously cannot be used for that. Asynchronous requests are identical to synchronous ones running on a separate thread, nothing special about them.

Mehrdad Afshari
Not looking to do a callback at all. Just want to know the port of the calling client. Good point about the asynch and thread, it's all happening on the client side.
Leeks and Leaks
See my answer: port number and IP address you may get will be meaningless under some circumstances. Perhaps you knew this and know you're not in that circumstance? Then please say so.
John Saunders
@John: and even if the client is not behind NAT, the port number is not very meaningful.
Mehrdad Afshari
What do you mean by meaningful?
Leeks and Leaks
It's unlikely to represent anything useful for an application layer protocol.
Mehrdad Afshari
A: 

You should be able to find it as "REMOTE_PORT" in the server variables of the Server object.

However, this port should pretty much always be random, and is only active for the Request/Response pair the client is making. It should be can't be used for asynchronous call backs. Even your webservice when calling to someother service will use a random port number to initiate the request from. The only static port in the communication is the receiving port at the server end of the TCP connection.

David McEwing
Can you elaborate? Are you talking about System.Web.HttpServerUtility that is found as part of the HttpContext object and as the Server property? How doyou obtain this value?
Leeks and Leaks
no. ServerVariables is part of the request object. See http://msdn.microsoft.com/en-us/library/system.web.httprequest.servervariables(VS.71).aspx
David McEwing
A: 

Some Network Address Translation-type devices will hide the actual "sending" port number from you. You would then have access to a useless IP address and a useless port number.

John Saunders