Reputation: 760
My local ip is : 192.168.0.70, External ip is : 192.168.0.50 : 60000
I want receive data from external ip sending to local ip. I use Socket class because i can send data using remote IPEndPoint. But when Udp connection closed, local ip's port dynamically changing. How can i receive data?
private static void UdpConnect()
{
try
{
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("192.168.0.50"), 60000);
Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
client.Connect(remoteEP);
byte[] sendbuffer = { 1, 2, 3 };
client.Send(sendbuffer);
byte[] receivebuffer = new byte[512];
client.Receive(receivebuffer);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Upvotes: 1
Views: 2115
Reputation: 451
If you need to listen on a specific local port, use Socket.Bind and then Socket.ReceiveFrom. You don't need the Socket.Connect call since UDP is a connectionless protocol.
Upvotes: 1