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()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
receiveSocket.Bind(bindEndPoint);
receiveSocket.Receive(recBuffer);
}
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("192.168.40.1"), 3838);
Recieve
is for TCP and not for UDP. Use ReceiveFrom
jgauffin 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
ethernet: 192.168.1.40
wireless: 192.168.1.42
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("192.168.1.40"), 0)
receiveSocket.ReceiveFrom(buffer, remoteEndpoint);
This restricts the source from which you accept data.