所请求的地址在其上下文中无效UDP数据包捕获程序

发布于 2025-02-09 19:24:35 字数 3348 浏览 0 评论 0原文

我正在尝试通过C#捕获调制解调器的UDP数据。但是,当我运行代码时,会发生以下异常:

请求的地址在其上下文中无效 system.net.sockets.socket.setmulticastoption(socketOptionName 选件名,多拟合MR)at system.net.sockets.socket.socket.setsocketoption(socketOptionlevel OptionLevel,socketOptionName optionName,Object OptionValue)at C:\ Users \ Muhammed中的caa..ctor() kara \ source \ repos \ udppragram \ udpprogram \ caa.cs:第25行此处是 Wireshark属性:来源IP:192.168.1.150 DEST IP:192.168.1.140 来源端口:32768 dest端口:50000

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.1.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(); 
        }
    }
}

I am trying to capture udp data from the modem by C#. But when I run the code the following exception occurs:

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 25 here are
Wireshark properties: source IP: 192.168.1.150 dest IP: 192.168.1.140
source port: 32768 dest port: 50000

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.1.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(); 
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文