如何在WCF中获取客户端IP地址和UDP端口?

时间:2012-06-28 09:20:03

标签: android ios iphone wcf ipad

我在WCf中开发了一些智能设备消耗的服务,例如Android等

当设备调用它们时,我在WCF服务中获得客户端(设备)端点(IpAddress,端口)。之后,我在该IPAddress和端口上发送UDP消息,但该UDP消息不会到达目的地。

可能有一些问题是从客户端提取端口,因为服务可能在HTTP上,我得到HTTP端口并在该端口不接受的端口上发送UDP消息。

请帮忙。

下面是提取客户端IPAddress和PORT并在该点上发送UDP消息的代码

OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
var endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
int nRemotePort = 0;
string sRemoteAddress = "";
if (endpointProperty != null)
{
     sRemoteAddress = endpointProperty.Address;
     nRemotePort = endpointProperty.Port;
}

//获得了IPAddress和Port,现在将UDP消息发送到地址

UdpClient udpClient = new UdpClient();
udpClient.Connect(sRemoteAddress, nRemotePort);
string msgTag = ((DateTime.Now.Month.ToString().PadLeft(2, '0') + DateTime.Now.Day.ToString().PadLeft(2, '0') + DateTime.Now.Year.ToString().PadLeft(4, '0')) + DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0')) + DateTime.Now.Second.ToString().PadLeft(2, '0') + DateTime.Now.Millisecond.ToString().PadLeft(3, '0');


string msgBody = "786022^Successfully Connected to Server, now send Data";
string msg = "8^" + msgTag + "^45^1^0^" + msgBody.Length + Convert.ToChar(2).ToString() + msgBody + Convert.ToChar(4).ToString();
byte[] sendBytes = Encoding.ASCII.GetBytes(msg);
udpClient.Send(sendBytes, sendBytes.Length);
udpClient.Close(); 

1 个答案:

答案 0 :(得分:1)

解开了这个谜,实际上我是从WCF服务中提取TCP端口并在该端口上发送我的UDP消息,这是错误的。

现在我必须在设备应用程序中粘贴UDP端口并将我的消息发送到从WCF提取的IPAddress,并确定UDP端口。

相关问题