Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UDP守护程序类:引用的对象类型不支持尝试的操作_C#_Sockets - Fatal编程技术网

C# UDP守护程序类:引用的对象类型不支持尝试的操作

C# UDP守护程序类:引用的对象类型不支持尝试的操作,c#,sockets,C#,Sockets,我正在C#中创建一个UDP守护进程类,在Visual Studio中设置断点后,我看到“所引用的对象类型不支持尝试的操作”。在this::ip::Address::ScopeId::base中。ScopeId引发异常System.Net.Sockets.SocketException。错误代码为10045/不支持操作 呼叫代码: namespace Foo.Tester { class Program { static void Main(string[] arg

我正在C#中创建一个UDP守护进程类,在Visual Studio中设置断点后,我看到“所引用的对象类型不支持尝试的操作”。在
this::ip::Address::ScopeId::base
中。ScopeId引发异常
System.Net.Sockets.SocketException
。错误代码为10045/不支持操作

呼叫代码:

namespace Foo.Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            var TestDaemon = new UDPDaemon();
            TestDaemon.port = 9999;
            TestDaemon.Start();
            ...
UDPDaemon类:

{
    public class UDPDaemon
    {

        public int receivedDataLength; 
        public byte[] data; 
        public IPEndPoint ip; 
        public Socket socket; 
        public IPEndPoint sender;
        public EndPoint Remote; 
        public string raw;
        public int port { get; set; }
        public LogRow row;


        public UDPDaemon() 
        {
            ip = new IPEndPoint(IPAddress.Any, port);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sender = new IPEndPoint(IPAddress.Any, 0);
            Remote = (EndPoint)(sender);
        }
        public void Start()
        {
            socket.Bind(ip);
            while (true)
            {
                data = new byte[1024];
                receivedDataLength = socket.ReceiveFrom(data, ref Remote);
                raw = Encoding.ASCII.GetString(data, 0, receivedDataLength);
                row = new LogRow(raw);
                //Will eventually move to Queue, but just print it for now
                Console.WriteLine(row.ClientIp);
            }
        }
    }
}
  • 导致此异常的原因是什么?异常的含义是什么
  • 为什么我只在VS中设置断点时才会看到异常
  • 我刚刚开始学习这门语言,所以如果代码中还有什么不对劲的地方,最好知道

  • 由于您希望在构造函数内使用端口,因此需要将其作为构造函数参数传递,而不是稍后进行设置,例如:

    public class UDPDaemon
    {
        public int receivedDataLength; 
        public byte[] data; 
        public IPEndPoint ip; 
        public Socket socket; 
        public IPEndPoint sender;
        public EndPoint Remote; 
        public string raw;
        public int Port { get; private set; }
        public LogRow row;
    
        public UDPDaemon(int port) 
        {
            Port = port;
            ip = new IPEndPoint(IPAddress.Any, port);
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sender = new IPEndPoint(IPAddress.Any, 0);
            Remote = (EndPoint)(sender);
        }
     ....
    

    未在
    ip=new IPEndPoint(IPAddress.Any,port)中设置端口这导致了套接字异常,所以这是我缺少的某种基本C#OO概念……啊,我现在明白了,ip是在对象实例的构造过程中分配的,而端口直到之后才分配。