确定端口消息源自 [英] Determine ports messages originated from
本文介绍了确定端口消息源自的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个正在运行的Windows服务,它实现了一个带有4个前缀的HTTP侦听器,每个前缀具有不同的端口号.从这些端口中的任何一个端口接收到消息时,我想确定消息源自哪个端口.任何帮助将不胜感激.
这是我的代码:
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;
}
}
}
推荐答案
.NET类ActionExecutingContext为ActionExecuting方法提供了上下文.我们可以使用ActionExecutingContext获取消息源端口,如下所示:
.NET class ActionExecutingContext provides the context for the ActionExecuting method. We can fetch the message originating port using ActionExecutingContext as:
ActionExecutionContext.HttpContextBase.Url.Port;
这篇关于确定端口消息源自的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文