简体   繁体   中英

How to get client IP address and UDP port in WCF?

I developed some services in WCf which are consumed by Smart device eg Android etc

I am getting client (Device) endpoint (IpAddress,port) in WCF service when they are called by Device. And after that I send a UDP message on that IPAddress and port but that UDP message donot reach to destination.

May be there is some problem which extracting port from Client as service could be on HTTP and I get the port of HTTP and send UDP message on that port which that port does not accept.

please help.

below is the code to extract client IPAddress and PORT and send UDP message on that point

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;
}

// got IPAddress and Port, now send UDP message to address

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(); 

Solved the mystery, Actually I was extracting TCP port from WCF service and send my UDP message on that port, which was wrong.

Now I have to stick a port for UDP in Device application and sending my messages to IPAddress extracted from WCF and that decided UDP port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM