UDP广播系统无法在单独的计算机上进行通信 [英] UDP broadcasting system won't communicate on separate computers

查看:141
本文介绍了UDP广播系统无法在单独的计算机上进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#广播信标,该信标应该将服务器信息广播到所有侦听设备.发送的信息将包含WCF服务的URL,名称空间,必需参数列表等信息.我现在所拥有的是一个发送方和接收方,当它们在同一台计算机上时,它们可以很好地对话.但是,一旦将发件人放到接收者以外的另一台计算机上,发件人就发送了它的消息,但我的接收者再也没有收到它.没有异常被抛出,并且两台计算机上的防火墙都被禁用.

I am working on a broadcast beacon in C# that is supposed to broadcast server information to all listening devices. The information sent will contain information like the URL of a WCF service, the namespace, a list of required arguments etc. What I have right now is a sender and receiver that can talk perfectly fine when they are on the same computer. However, once I put the sender on another computer than my receiver, the sender sends its message but my receiver never gets it. There are no exceptions being thrown, and the firewall is disabled on both machines.

http://codeidol.com/csharp/csharp -network/IP-Multicasting/What-Is-Broadcasting/是我从中获取代码的地方.

http://codeidol.com/csharp/csharp-network/IP-Multicasting/What-Is-Broadcasting/ is where I got my code from.

发件人:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
            ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
            IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 9050);
            byte[] data = Encoding.ASCII.GetBytes("This is a test message");
            sock.SendTo(data, iep);
            sock.Close();
        }
    }
}

接收器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPBroadcastReciever
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket sock = new Socket(AddressFamily.InterNetwork,
            SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
            sock.Bind(iep);
            EndPoint ep = (EndPoint)iep;
            Console.WriteLine("Ready to recieve");
            byte[] data = new byte[1024];
            int recv = sock.ReceiveFrom(data, ref ep);
            string stringData = Encoding.ASCII.GetString(data, 0, recv);
            Console.WriteLine("Received: {0} from: {1}", stringData, ep.ToString());
            sock.Close();
            Console.ReadLine();
        }
    }
}

有人知道我缺少什么使这两个人可以在两台不同的计算机上通话吗?它们在同一子网(192.168.1.x)

Does anyone know of anything I am missing that would enable these two to talk on two different computers? They are on the same subnet (192.168.1.x)

谢谢 尼克·朗

推荐答案

使用多播而不是广播可能会更好.广播数据包经常被路由器立即丢弃.在239.0.0.0/24块中的某个位置选择一个IP地址作为多播地址;这是为组织本地邮件保留的,因此,请立即选择一个号码并坚持下去.

You would probably be better off using multicast rather than broadcasting; broadcast packets are often dropped immediately by routers. Pick an IP address somewhere in the 239.0.0.0/24 block as your multicast address; this is reserved for organisation local messages, so just pick a number out of the air and stick with it.

您需要让发送方将其数据包发送到该地址,并使接收方加入多播组以接收它们.要加入多播组,请在您的套接字上调用它:

You need to have your sender send its packets to this address and have your receiver join the multicast group to receive them. To join the multicast group call this on your socket:

sock.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
    new MulticastOption(theMulticastIp));

有关在C#中使用多播的更多信息,此处

There's plenty more information about using multicast in C# here.

这篇关于UDP广播系统无法在单独的计算机上进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆