C# UDP接收不到其他电脑发来的数据

做了一个局域网UDP传输数据的东西,在同一台电脑上测试的时候可以收到数据,发送端放到别的电脑上就收不到了,不知道是什么原因,求教各位大神!!

cls_interface.localHost cls_interface._socketRev 是static的
这是接收的代码

 cls_interface.localHost = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
cls_interface._socketRev = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
cls_interface._socketRev.Bind(cls_interface.localHost);
while (!cls_interface.IsStopRev)
            {
                if (!cls_interface.IsSuspendRev)
                {
                    byte[] buffer = new byte[1024];
                    int len = 0;

                    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint Remote = (EndPoint)remoteEP;
                    len = cls_interface._socketRev.ReceiveFrom(buffer, ref Remote);

                    //获取读取到的信息
                    if (len > 0)
                    {
                        byte[] revbts = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            revbts[i] = buffer[i];
                        }
                        get_udp_data(revbts);
                        pointsCount++;
                    }
                }//判断是否暂停
            }

这是发送代码

 public void SendData()
        {
            double x = 0;
            double i = 0;
            while (true)
            {
                i+=0.5;
                revdData revdata;

                /* revdata  结构体赋值*/
                //将结构体转换成byt[]
                Type type = typeof(revdData);
                int size = Marshal.SizeOf(type);
                IntPtr buffer=Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(revdata, buffer, false);
                byte[] bts=new byte[size];
                Marshal.Copy(buffer,bts,0,size);               

                double sd = Math.Sin(x);
                _socket.SendTo(bts, new IPEndPoint(IPAddress.Parse("192.168.1.2"), 8001));

                Marshal.FreeHGlobal(buffer);

                Thread.Sleep(100);
                x += 0.1;
            }
        }




3个回答

自顶!求帮助!!!!!!!!!

        IPEndPoint ip = new IPEndPoint(IPAddress.Any, 8001);
        Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

问题解决。
cls_interface.localHost = new IPEndPoint(本机IP 8001);
cls_interface._socketRev = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐