• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Question Bind IP-Endpoint to Socket

TBMSam

New member
Joined
Dec 12, 2018
Messages
3
Programming Experience
Beginner
Hello everyone,

on my local network, there is a udp broadcast sent by a server every 5 seconds. It is 120 Byte long.
I am trying to set a socket in order to consume it:

Code:
IPAddress ip = IPAddress.Parse("192.168.127.162");
IPEndPoint ipe = new IPEndPoint(ip, 55555);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
{
    ReceiveBufferSize = 120 
};
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30);
socket.Bind(ipe);
My local cmd -> /ipconfig shows the following:



Unfortunately, I am getting error message in the binding line:

System.Net.Sockets.SocketException: The requested address is not valid in this context
What could be the problem? Wrong IP format? Or am I doing something wrong when instantiating?

Would be really happy if someone could help me anyhow.

Best regards
 
Last edited:

TBMSam

New member
Joined
Dec 12, 2018
Messages
3
Programming Experience
Beginner
Unfortunately I shared the wrong picture I am sorry. My cmd -> /ipconfig-settings are as follows:
own_ip.png
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
482
Location
Norway
Programming Experience
10+
Use IPAddress.Any instead. The code can also be simplified with UdpClient class:
var client = new UdpClient(55555);
client.Client.ReceiveBufferSize = 120;
 
Top Bottom