Azhar
Azhar

Reputation: 20678

.net: send UDP message does not working on Public IP

I am trying to send UDP message through WCF .net application to Android device, which working fine in LAN(Local Area Network) environment

but When I deploy that WCF service (MyService.svc) to PUblic IP (IIS) it sends UDP packet successfully but deice does not receive that packet.

Code to send UDP message

public void SendUDPMessage(IPEndPoint ipeSender, string Message)
{
    byte[] sendBytes = Encoding.ASCII.GetBytes(Message);
    if (serverSocket == null)
       serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    serverSocket.BeginSendTo(sendBytes, 0, sendBytes.Length, SocketFlags.None, ((EndPoint)ipeSender), new AsyncCallback(OnSend), null);

}

Upvotes: 0

Views: 381

Answers (1)

Seth Noble
Seth Noble

Reputation: 3303

Assuming the Android device is on a wireless/cellular network, it is effectively behind an NAT firewall and so any network traffic which originates externally will be blocked.

In order to punch through the NAT/firewall, the Android device needs to first send a message to the server. Then the server must respond to the exact address and port number that are given as the UDP messages' source.

NAT hole punching is a messy subject, which a lot of subtle issues. But it should work so long as the server has a real public IP address, the mobile device sends the first message, and you aren't doing any of the dozens of other things that can cause a UDP packet to be lost (packet larger than 1400 bytes, first few bytes look like a Teredo tunnel, server using a blocked port, sending data too quickly, server firewall misconfigured, etc.).

Upvotes: 1

Related Questions