Question

I've written a proxy server using System.Net.HttpListener and C# (Windows 7+, .Net 4.0). I would like to identify which process is sending the request (the sender is guaranteed to be on the same machine).

Good old @EricLaw has something that's almost works, http://blogs.msdn.com/b/fiddler/archive/2013/01/09/10138573.aspx but... only if I subtract 1 from the Port #:

HttpListenerContext context = GetContext();
HttpListenerRequest request = context.Request;
int pid = 0;
if (request.IsLocal)
{
    int port = context.Request.RemoteEndPoint.Port;
    pid = Winsock.MapLocalPortToProcessId(port - 1)
}
return pid;

If I don't subtract 1 the request originates from PID=4, the System process.

Is the subtraction the thing I should be doing? Or is there a different way to determine which process sent the request?

Was it helpful?

Solution

The code shared on my blog works just fine; it's used in Fiddler, which is used by millions of people.

You should dump the entire port list and see what's going on; perhaps there's a problem in the value returned from context.Request.RemoteEndPoint.Port.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top