问题
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/ is where I got my code from.
Sender:
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();
}
}
}
Receiver:
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();
}
}
}
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)
Thanks Nick Long
回答1:
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));
There's plenty more information about using multicast in C# here.
回答2:
I had the similar problem, nothing seemed to work in all the code I saw here. when I started my program there was this firewall window that pops up telling you the firewall has blocked some features.
my problem was I was clicking allow access without ticking the check box that says "Private networks, suck as my home or work network". you can change this later of course in your firewall setting and tick that check box. both de server and the other machine must have that check box checked. Or at least that's what makes my mine work.
Also i had to change my broadcast IP address to for example 192.168.1.255. My router does block the recommended by my book 224.0.0.0 - 239.255.255.255;
来源:https://stackoverflow.com/questions/9422522/udp-broadcasting-system-wont-communicate-on-separate-computers