Click here to Skip to main content
15,742,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an windows service running, implementing an HTTP Listener with 4 prefixes, each with different port numbers. When a message is received from either of those ports, I would like to determine which port the message originated from. Any help would be appreciated.

Here is my code:

public void Listen(string[] prefix)
        {
            httpListener = new HttpListener();

            foreach (string s in prefix)
            {
                httpListener.Prefixes.Add(s);
            }

            //start the listener
            httpListener.Start();

            startListening();
        }

        public void startListening()
        {
            while (isTrue)
            {
                HttpListenerContext httpContext = httpListener.GetContext();
                HttpListenerRequest request = httpContext.Request;
                //string portNumber = ??????

                switch (portNumber)
                {
                    case 1:
                        //do something
                        break;
                    case 2:
                        //do something
                        break;
                    case 3:
                        //do something
                        break;
                    default:
                        Console.WriteLine("invalid port");
                        break;
                }
            }
        }
Posted
Updated 7-Mar-12 21:54pm
v2
Comments
Herman<T>.Instance 8-Mar-12 3:24am    
first show some code. We do not know now what you have setup. We cannot see sharp now so improve your question.

1 solution

.NET class ActionExecutingContext provides the context for the ActionExecuting method. We can fetch the message originating port using ActionExecutingContext as:

C#
ActionExecutionContext.HttpContextBase.Url.Port;
 
Share this answer
 
Comments
Andrew797 9-Mar-12 2:02am    
what reference should I add? On msdn it says the System.Web.Mvc reference, but I cannot find it. and what using statement?
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900