UDP C# connection

Go To StackoverFlow.com


I have a program shown below:

Socket receiveSocket = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Dgram, ProtocolType.Udp);

EndPoint bindEndPoint = new IPEndPoint(IPAddress.Any, 3838);

byte[] recBuffer = new byte[256];

public Form1()

private void button1_Click(object sender, EventArgs e)

and it is working but when I want to just listen to a specific IP address it does not work, it throws an exeption "the requested address is not valid in context"

new code: EndPoint bindEndPoint = new IPEndPoint(IPAddress.Parse(""), 3838);

2012-04-04 07:10
by mefmef
did you mean 'listen on a specific IP' instead of 'listen to'? because that's what your new sample suggests you want to do. - mtijn 2012-04-04 08:27
Are you sure that your computer have that IP address? Also: Recieve is for TCP and not for UDP. Use ReceiveFromjgauffin 2012-04-04 08:28


The IP addresses you are binding to, should be one of the IP addresses that are actually assigned to the network interfaces on your computer.

Let's say you have an ethernet interface and a wireless interface



When calling the Bind method on your socket, you are telling it on which interface you want to listen for data. That being either 'any of them' or just the ethernet interface for example.

I'm guessing this is not what you are looking for?

Perhaps you are trying to restrict who can make a connection to your server

So whate you may be looking for is

var remoteEndPoint = new IPEndPoint(IPAddress.Parse(""), 0)
receiveSocket.ReceiveFrom(buffer, remoteEndpoint);

This restricts the source from which you accept data.

2012-04-04 08:37
by TimothyP
IPAddress.Parse("") should be IPAddress.Parse("") - Mystic Lin 2017-08-23 03:41
Nice catch! Thank you @MysticLi - TimothyP 2017-08-24 17:19