UDP Client Exception The requested address is not valid in its context

Muhammed Kara 1 Reputation point
2022-06-20T12:07:21.24+00:00

I am trying to capture raw UDP data from a 192 starting ip adress and wrote a code in c sharp #. But when I run the code I see an exception like "The requested address is not valid in its context at System.Net.Sockets.Socket.setMulticastOption(SocketOptionName optionName, MulticastOption MR) at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Object optionValue) at CAA..ctor() in C:\Users\Muhammed Kara\source\repos\UDPprogram\UDPprogram\CAA.cs:line 26"

here is my code;

using System;  
using System.Net;  
using System.Net.Sockets;  
  
class CAA  
{  
  
    private Socket UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  
    private IPAddress Target_IP;  
    private int Target_Port;  
    public static int bPause;  
  
    public CAA()  
    {  
        Target_IP = IPAddress.Parse("192.168.0.140");  
        Target_Port = 9999;  
  
        try  
        {  
            IPEndPoint LocalHostIPEnd = new  
            IPEndPoint(IPAddress.Any, Target_Port);  
            UDPSocket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);  
            UDPSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);  
            UDPSocket.Bind(LocalHostIPEnd);  
            UDPSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 0);  
            UDPSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new  
            MulticastOption(Target_IP));  
            Console.WriteLine("Starting Recieve");  
            Recieve();  
        }  
        catch (Exception e)  
        {  
            Console.WriteLine(e.Message + " " + e.StackTrace);  
        }  
    }  
  
    private void Recieve()  
    {  
        try  
        {  
            IPEndPoint LocalIPEndPoint = new  
            IPEndPoint(IPAddress.Any, Target_Port);  
            EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint;  
            StateObject state = new StateObject();  
            state.workSocket = UDPSocket;  
            Console.WriteLine("Begin Recieve");  
            UDPSocket.BeginReceiveFrom(state.buffer, 0, state.BufferSize, 0, ref LocalEndPoint, new AsyncCallback(ReceiveCallback), state);  
        }  
        catch (Exception e)  
        {  
            Console.WriteLine(e.ToString());  
        }  
    }  
  
    private void ReceiveCallback(IAsyncResult ar)  
    {  
  
        IPEndPoint LocalIPEndPoint = new  
        IPEndPoint(IPAddress.Any, Target_Port);  
        EndPoint LocalEndPoint = (EndPoint)LocalIPEndPoint;  
        StateObject state = (StateObject)ar.AsyncState;  
        Socket client = state.workSocket;  
        int bytesRead = client.EndReceiveFrom(ar, ref LocalEndPoint);  
  
  
  
        client.BeginReceiveFrom(state.buffer, 0, state.BufferSize, 0, ref LocalEndPoint, new AsyncCallback(ReceiveCallback), state);  
    }  
  
  
    public class StateObject  
    {  
        public int BufferSize = 512;  
        public Socket workSocket;  
        public byte[] buffer;  
  
        public StateObject()  
        {  
            buffer = new byte[BufferSize];  
        }  
    }  
  
}  
  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
  
namespace UDPprogram  
{  
    internal class Program  
    {  
        static void Main(string[] args)  
        {  
            CAA udpport = new CAA();  
            Console.ReadLine();   
        }  
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,020 questions
{count} votes