【问题标题】:UDP to server does not workUDP到服务器不起作用
【发布时间】:2013-12-09 21:08:34
【问题描述】:

在工作中,我必须路由一些使用 UDP 传入的数据。我遵循了一些教程,例如this blogthis tutorial,并制作了两个控制台应用程序,一个用于发送 UDP 消息(客户端),一个用于读取它们(侦听器)。 当我输入 IP 地址 127.0.0.1 (localhost) 时,一切顺利,但没有其他地址有效。如果我使用我的真实本地 IP 地址,我就没有运气。由于我在本地没有静态 IP,因此我还尝试将侦听器安装在具有静态 IP 的服务器上。在这样做时,我确保将规则添加到 Windows 防火墙入站规则集中。仍然没有运气。有任何想法吗?这是我的代码:

/西蒙/

客户

    class Client
{
    private readonly Socket _sendingSocket;
    private readonly IPEndPoint _receiverEndpoint;
    private static int outboundPortNumber = int.Parse(ConfigurationManager.AppSettings["OutgoingPortNumber"]);

    public Client()
    {
        var receiverAddress = IPAddress.Parse(ConfigurationManager.AppSettings["IpOfReceiver"]);
        _receiverEndpoint = new IPEndPoint(receiverAddress, outboundPortNumber);
        _sendingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        _sendingSocket.EnableBroadcast = true;
    }

    public void Transmit()
    {
        bool done = false;
        while (!done)
        {
            Console.WriteLine("Hit q to quit");
            var keypress = Console.ReadKey();

            if (keypress.KeyChar.ToString().ToLower() == "q")
                done = true;
            else
                SendTextToServer();
        }
    }

    private void SendTextToServer()
    {
        Console.WriteLine("Enter text to send");
        var textToSend = Console.ReadLine();
        byte[] send_buffer = Encoding.ASCII.GetBytes(textToSend);
        try
        {
            _sendingSocket.SendTo(send_buffer, _receiverEndpoint);
        }
        catch (Exception send_exception)
        {
            Console.WriteLine(" Exception {0}", send_exception.Message);
        }
    }
}

听众

   class Listener
{
    private bool _started;
    private ManualResetEvent _stop;
    private UdpClient _client;
    private IPEndPoint _endPoint;
    private Thread _workingThread;

    private static int inboundPortNumber = int.Parse(ConfigurationManager.AppSettings["IncomingPortNumber"]); 

    public void Start()
    {
        _started = true;
        _stop = new ManualResetEvent(false);
        InitializeUdpClient();
        InitializeWorkingThread();
    }

    private void InitializeUdpClient()
    {
        _endPoint = new IPEndPoint(IPAddress.Any, inboundPortNumber);
        _client = new UdpClient(_endPoint);
    }

    private void InitializeWorkingThread()
    {
        _workingThread = new Thread(WorkerFunction);
        _workingThread.Name = "WorkingThread";
        _workingThread.Start();
    }

    private void WorkerFunction()
    {
        while (_started)
        {
            var res = _client.BeginReceive(iar =>
            {
                if (iar.IsCompleted)
                {
                    byte[] receivedBytes = _client.EndReceive(iar, ref _endPoint);
                    OutputToConsole(receivedBytes);


                }
            }, null);

            if (WaitHandle.WaitAny(new[] { _stop, res.AsyncWaitHandle }) == 0)
            {
                break;
            }
        }
    }

    private void OutputToConsole(byte[] receivedBytes)
    {
        string receivedPacket = Encoding.ASCII.GetString(receivedBytes);
        Console.WriteLine("{0} received at {1}", receivedPacket, DateTime.Now.ToString("hhMMss.ffff"));
    }
}

--编辑--

其中一位 cmets 建议我应该查看一些日志记录。使用 Microsoft Network Monitor 捕获我的数据流量,我尝试了以下方法: (1) 在开发机器上:从我的开发机器发送到 127.0.0.1 时(所以从本地主机到本地主机)没有记录任何数据。我觉得这很奇怪,因为这是接收消息并很好地打印到控制台的唯一场景。 (2) 当我从我的开发机器发送到服务器时,在我的开发机器上捕获了一些出站数据,并且在服务器上也有传入数据。我在服务器上运行了 netstat -b,它根本没有显示任何 UDP 协议。由于在 InitializeUdpClient() 方法中,UdpClient 带有一个 IPEndpoint,所以我应该绑定到该端口。为什么当我运行 netstat 时它不显示?根据这个新信息,您对为什么监听器没有收到任何数据有什么建议吗?

/西蒙/

【问题讨论】:

  • 您对数据包捕获的故障排除是什么样的?
  • 您的错误报告只包含诸如“不起作用”或“没有运气”之类的短语。发生什么了?你有错误吗?是否创建了套接字?

标签: udp udpclient


【解决方案1】:

抱歉,问题与防火墙有关。我已启用在服务器上设置 nibound 规则并在我的开发机器上设置出站规则,但公司代理默默地阻止了流量。当为客人切换到无线网络时,我的流量到达了服务器。

西蒙

【讨论】:

    猜你喜欢
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    相关资源
    最近更新 更多