The client sends a UDP packet to a server on the Internet, but it could not receive UDP packets from the server


Client sends a UDP Packet to a server over the internet, but it could not recieve any UDP packages from the server.

Server has a valid IP and I'm connected to the internet via ADSL

In both server and client these codes are used:

Send Packet:

Socket sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep1 = new IPEndPoint(IPAddress.Parse(txt_IP.Text), Convert.ToInt32(txt_SendPort.Text));
byte[] data = Encoding.ASCII.GetBytes("UDP");

sock1.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
sock1.SendTo(data, iep1);

Receive Packet:

Console.WriteLine("Listening to the port {0}", PortNumber);
sock1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, PortNumber);
EndPoint ep = (EndPoint)iep;
byte[] data1 = new byte[100];
int recv = sock1.ReceiveFrom(data1, ref ep);
String str_Data = Encoding.ASCII.GetString(data1, 0, recv);
String str_IP = ep.ToString().Substring(0, ep.ToString().IndexOf(":"));
Console.WriteLine("Received Succesfully: {0} - {1}", str_Data, str_IP);

My NIC IP is, But the server shows my IP is and send the reply to ( is the IP of access point). How can I change the code in the server side to send bank the packet to the client correctly?

By access point, is that your router? You said ADSL, so I'll assume it is.

Your router spans 2 networks. The Internet is on 1 side in the form of your ISP & your internal network is on the other. This is why the server sees your router IP, not your NIC IP.

You need to define a port forward rule on the router to send inbound traffic on the port you're using onwards to your PC. Don't use broadcast either - this is for contacting a number of NICs on a subnet.

Strictly speaking this is more of network issue rather than a programming one.