3

我对下面的代码有疑问。它的作用是使用 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 个字节,但第一次 Available 大于 0,它的值为 38。接下来的 Receive 调用只获取 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.
4

2 回答 2

4
socketReceive.Bind(new IPEndPoint(IPAddress.Any, Port));

通过绑定到您的所有 IP 地址,您将获得本地环回以及通过网络。

于 2009-07-29T10:12:39.030 回答
0

我建议您先在指定的目标 IP 上尝试此操作。这样,网络拓扑的影响会更小,当一切都按您期望的方式运行时,您可以将其移动到广播地址。

于 2009-07-29T10:10:37.410 回答