我的代码存在问题。它的作用是使用UDP广播发送当前日期/时间并收听此消息。我目前使用此代码是本地的,我在同一台计算机上使用同一应用程序中的发送和接收。我还没有在两台电脑之间试过它。
public class Agent
{
public static int Port = 33333;
public delegate void OnMessageHandler(string message);
Socket socketSend;
Socket socketReceive;
bool receiving = false;
Thread receiveThread;
public event OnMessageHandler OnMessage;
public Agent()
{
socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketSend.EnableBroadcast = true;
socketSend.Connect(new IPEndPoint(IPAddress.Broadcast, Port));
socketReceive = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
socketReceive.EnableBroadcast = true;
socketReceive.Bind(new IPEndPoint(IPAddress.Any, Port));
}
public void Start()
{
Console.WriteLine("Agent started!");
receiving = true;
receiveThread = new Thread(new ThreadStart(Receive));
receiveThread.IsBackground = true;
receiveThread.Start();
}
public void Stop()
{
receiving = false;
socketSend.Close();
socketReceive.Close();
receiveThread.Join();
Console.WriteLine("Agent ended.");
}
public void Send(string message)
{
Console.WriteLine("Sending : {0}", message);
byte[] bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, Encoding.Unicode.GetBytes(message));
socketSend.Send(bytes);
Console.WriteLine("Message sent.");
}
protected void Receive()
{
Console.WriteLine("Thread started!");
while (receiving)
{
try
{
if (socketReceive.Available > 0)
{
byte[] bytes = new byte[socketReceive.Available];
Console.WriteLine("Waiting for receive...");
int bytesReceived = socketReceive.Receive(bytes, SocketFlags.None);
Console.WriteLine("Bytes received : {0}", bytesReceived);
string message = Encoding.UTF8.GetString(bytes);
Console.WriteLine("Received message : {0}", message);
OnMessage(message);
}
else
{
Console.Write(".");
Thread.Sleep(200);
}
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.Interrupted)
{
break;
}
else
{
throw;
}
}
}
Console.WriteLine("Thread ended.");
}
}
我遇到的问题是它发送一次数据(当我点击一个按钮时)但它收到的数量是预期的两倍。例如,正常日期/时间是19字节长,但第一次可用是> 0,其值为38.接收调用仅捕获19个字节,循环继续接下来的19个字节。这意味着我收到了我的消息并且两次,当然我只想要它一次。
输出示例:
Agent started!
Thread started!
..........Sending : 29/07/2009 12:05:04
Message sent.
Waiting for receive...
Bytes received : 19
Received message : 29/07/2009 12:05:04
Waiting for receive...
Bytes received : 19
Received message : 29/07/2009 12:05:04
......Thread ended.
Agent ended.
答案 0 :(得分:4)
socketReceive.Bind(new IPEndPoint(IPAddress.Any, Port));
通过绑定到您的所有IP地址,您将获得本地环回并通过网络。
答案 1 :(得分:0)
我建议您先在指定的目标IP上尝试此操作。这样,网络拓扑结构的影响就会减小,只要一切按预期方式运行,您就可以将其移动到广播地址。