我在 WCf 中开发了一些服务,供智能设备使用,例如安卓等
当设备调用它们时,我在 WCF 服务中获取客户端(设备)端点(IpAddress、端口)。之后,我在该 IP 地址和端口上发送了一条 UDP 消息,但该 UDP 消息没有到达目的地。
从客户端提取端口可能存在一些问题,因为服务可能在 HTTP 上,我获取 HTTP 端口并在该端口不接受的端口上发送 UDP 消息。
请帮忙。
下面是提取客户端IP地址和端口并在该点发送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;
}
//得到 IP 地址和端口,现在发送 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();
最佳答案
解开了谜团,实际上我是从 WCF 服务中提取 TCP 端口并在该端口上发送我的 UDP 消息,这是错误的。
现在我必须在设备应用程序中为 UDP 保留一个端口,并将我的消息发送到从 WCF 中提取的 IPAddress 并确定 UDP 端口。
关于android - 如何在 WCF 中获取客户端 IP 地址和 UDP 端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11241543/