[英]C# UDP Handshake error
I have a program that will broadcast on 255.255.255.255
. 我有一个程序,它将在
255.255.255.255
广播。 First it will broadcast a message to request for LOCATION then listens for the reply. 首先,它将广播一条消息以请求LOCATION,然后侦听答复。 If the broadcast is starting with
LOCATION:
it will get the location string. 如果广播从
LOCATION:
开始LOCATION:
它将获取位置字符串。 Here is my code: 这是我的代码:
UDPCommunication Class
class UDPCommunication
{
public void BroadcastMessage(string message, int port)
{
BroadcastMessage(Encoding.ASCII.GetBytes(message), port);
}
private static void BroadcastMessage(byte[] message, int port)
{
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp))
{
sock.EnableBroadcast = true;
sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
var iep = new IPEndPoint(IPAddress.Broadcast, port);
sock.SendTo(message, iep);
}
}
public void ReceiveBroadcastMessage(Action<EndPoint, string> receivedAction, int port)
{
ReceiveBroadcastMessage((ep, data) =>
{
var stringData = Encoding.ASCII.GetString(data);
receivedAction(ep, stringData);
}, port);
}
private void ReceiveBroadcastMessage(Action<EndPoint, byte[]> receivedAction, int port)
{
using (var sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
var ep = new IPEndPoint(IPAddress.Any, port) as EndPoint;
sock.Bind(ep);
while (true)
{
var buffer = new byte[1024];
var recv = sock.ReceiveFrom(buffer, ref ep);
var data = new byte[recv];
Array.Copy(buffer, 0, data, 0, recv);
receivedAction(ep, data);
Thread.Sleep(500);
Application.DoEvents();
}
}
}
}
Form1.cs
public event EventHandler BroadcastMessageArrived;
public event EventHandler LocationReceived;
string location = null;
string ReceivedMessage = null;
bool WorkDone = false;
UDPCommunication UDP = new UDPCommunication();
private void Form1_Load(object sender, EventArgs e)
{
BroadcastMessageArrived += new EventHandler(OnBroadcastMessageArrived);
LocationReceived += new EventHandler(Form1_LocationReceived);
backgroundWorker1.RunWorkerAsync();
UDP.BroadcastMessage("GATE_REQUEST_LOCATION", 15000);
while (!WorkDone) ;
LogGlobally(string.Format("Location now set to: ", Gate_Application.Properties.Settings.Default.Location));
}
void OnBroadcastMessageArrived(object sender, EventArgs e)
{
Console.WriteLine("Message received: {0}", ReceivedMessage); // Never being called
if (ReceivedMessage.StartsWith("LOCATION:"))
{
string[] LocationString = ReceivedMessage.Split(':');
location = LocationString[1];
EventHandler handler = LocationReceived;
if (handler != null)
{
handler(this, e);
}
return;
}
}
void Form1_LocationReceived(object sender, EventArgs e)
{
Gate_Application.Properties.Settings.Default.Location = location;
Gate_Application.Properties.Settings.Default.Save();
WorkDone = true;
return;
}
With my code above, I tried sending a broadcast LOCATION:SZ
but it never showed the message on the console. 使用上面的代码,我尝试发送广播的
LOCATION:SZ
但是它从未在控制台上显示该消息。 I suspect the problem is with the while loop which it won't breakoff even WorkDone
is =true
Please help! 我怀疑问题出在while循环上,即使
WorkDone
=true
也不会中断,请帮忙! Thanks! 谢谢!
I am not clear - did you even receive the message? 我不清楚-您是否收到了消息? If not:
如果不:
Almost all network hardware drops packets addressed 255.255.255.255 unconditionally (imagine how one could flood networks if they didn't). 几乎所有网络硬件都无条件丢弃寻址到255.255.255.255的数据包(想象一下,如果没有,网络将如何泛洪)。
Try broadcast to a more specific subnet, eg: 192.168.0.255 尝试广播到更特定的子网,例如:192.168.0.255
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.