ASP.NET UDP 套接字代码在开发中有效,但在 IIS 上的生产中无效

发布于 2024-08-14 16:54:15 字数 3172 浏览 6 评论 0原文

我有以下 UDP 广播侦听器作为静态组件在 ASP.NET Web 应用程序的单独线程中运行。为什么我要这样做确实不重要,但部署后不起作用的原因让我感到困惑。我确实有几个发送 UDP 广播的控制台应用程序,并且我已经编码测试并确认了该线程及其代码在 VS2005 开发 Web 服务器上的 Visual Studio 下运行时可以正常工作,但是当我编译代码并将其部署到另一台计算机时并在 IIS 下运行它 - 它停止工作。此外,它没有错误,只是不起作用。

log4net 日志记录似乎也只能在 Init() 方法中工作,而不能在线程中工作(这很奇怪)。但我在线程中添加了一些 File.AppendAllText,它似乎正在执行,但据我所知,它停止在 byte[] receive = mUdpClient 行上.Receive(ref mGroupEP);

我已确保端口已打开并在控制台应用程序中进行了测试(有效),我已检查权限,甚至使默认身份使用本地系统用户帐户,但是还是什么都没有。我检查了防火墙,检查了事件日志 - 什么也没有。没有错误,只是不工作。

我想知道这是否可能与 IIS 作为安全措施关闭 System.Net 请求有关,但我找不到任何可能支持该假设的内容。

  public class UDPBroadcastListener {

    #region Members

    private ILogger mLogger = NullLogger.Instance;

    private readonly int mPort;
    private UdpClient mUdpClient;
    private IPEndPoint mGroupEP;

    public event EventHandler<GenericEventArgs<byte[]>> Received;

    private Thread mThread;

    #endregion

    public UDPBroadcastListener(int port) {
      mPort = port;
    }

    public void Init() {
      try {
        Logger.WarnFormat("Setting up the UDP packet listener on port {0}", mPort);
        mGroupEP = new IPEndPoint(IPAddress.Any, mPort);
        mUdpClient = new UdpClient();
        mUdpClient.EnableBroadcast = true;
        mUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket,
                                          SocketOptionName.ReuseAddress,
                                          true);
        mUdpClient.Client.Bind(mGroupEP);
        Logger.InfoFormat("Successfully bound the UDP packet listener to the the port.");

        mThread = new Thread(UpdateThread);
        mThread.IsBackground = true;
        Logger.Info("Starting the background listener thread.");
        mThread.Start();
        Logger.InfoFormat("Background listener thread started ok.");
      } catch (Exception e) {
        Logger.Error(e.Message, e);
      }
    }

    public void Close() {
      if (mThread != null) {
        mThread.Abort();
      }

      if (mUdpClient != null) {
        mUdpClient.Close();
        mUdpClient = null;
      }
    }

    public Thread Thread {
      get { return mThread; }
    }

    private void UpdateThread() {
      Logger.Info("Listener thread started.");
      while (true) {
        try {
          Logger.Info("Waiting for broadcast");

          byte[] received = mUdpClient.Receive(ref mGroupEP);

          Logger.InfoFormat("Received {0} bytes", received.Length);
          OnReceived(received);
        } catch (Exception ex) {
          Logger.Error(ex.Message, ex);
        }
      }
    }

    protected void OnReceived(byte[] pData) {
      EventHandler<GenericEventArgs<byte[]>> handler = Received;
      if (handler != null) Received(this, new GenericEventArgs<byte[]>(pData));
    }

    public virtual ILogger Logger {
      get { return mLogger; }
      set { mLogger = value; }
    }

    #region Dispose

    private bool mDisposed = false;

    public void Dispose() {
      if (mDisposed) return;

      mDisposed = true;

      Close();
    }

    ~UDPBroadcastListener() {
      Dispose();
    }

    #endregion

  }
}

我现在变得非常绝望。请帮忙。 :(

I have the following UDP broadcast listener running as a static component in a seperate thread on an ASP.NET web application. Why I would do this is really, unimportant, but the reason why this wont work when deployed baffles me. I do have several console applications sending UDP broadcasts, and I've coded tested and confirmed this thread and its code working when running under Visual Studio on the VS2005 development web server, but the moment that I compile the code and deploy it to another machine and run it under IIS - it stops working. Furthermore, it doesnt ERROR, it just doesn't work.

The log4net logging also seems to only work in the Init() method, but not the thread (which is weird). But I have added some File.AppendAllText's to the thread and it DOES seem to be executing, but as far as I can tell, it stops on the line byte[] received = mUdpClient.Receive(ref mGroupEP);

I've made sure the port is open and tested in a console app (which works), I've checked permissions, even made the default identity use the Local System user account, but still nothing. I've checked the firewall, checked the event log - nothing. No errors, just not working.

I wondered if it might be somehting to do with IIS shutting down System.Net requests as a security meassure, but I can't find anything that might support that hypothesis.

  public class UDPBroadcastListener {

    #region Members

    private ILogger mLogger = NullLogger.Instance;

    private readonly int mPort;
    private UdpClient mUdpClient;
    private IPEndPoint mGroupEP;

    public event EventHandler<GenericEventArgs<byte[]>> Received;

    private Thread mThread;

    #endregion

    public UDPBroadcastListener(int port) {
      mPort = port;
    }

    public void Init() {
      try {
        Logger.WarnFormat("Setting up the UDP packet listener on port {0}", mPort);
        mGroupEP = new IPEndPoint(IPAddress.Any, mPort);
        mUdpClient = new UdpClient();
        mUdpClient.EnableBroadcast = true;
        mUdpClient.Client.SetSocketOption(SocketOptionLevel.Socket,
                                          SocketOptionName.ReuseAddress,
                                          true);
        mUdpClient.Client.Bind(mGroupEP);
        Logger.InfoFormat("Successfully bound the UDP packet listener to the the port.");

        mThread = new Thread(UpdateThread);
        mThread.IsBackground = true;
        Logger.Info("Starting the background listener thread.");
        mThread.Start();
        Logger.InfoFormat("Background listener thread started ok.");
      } catch (Exception e) {
        Logger.Error(e.Message, e);
      }
    }

    public void Close() {
      if (mThread != null) {
        mThread.Abort();
      }

      if (mUdpClient != null) {
        mUdpClient.Close();
        mUdpClient = null;
      }
    }

    public Thread Thread {
      get { return mThread; }
    }

    private void UpdateThread() {
      Logger.Info("Listener thread started.");
      while (true) {
        try {
          Logger.Info("Waiting for broadcast");

          byte[] received = mUdpClient.Receive(ref mGroupEP);

          Logger.InfoFormat("Received {0} bytes", received.Length);
          OnReceived(received);
        } catch (Exception ex) {
          Logger.Error(ex.Message, ex);
        }
      }
    }

    protected void OnReceived(byte[] pData) {
      EventHandler<GenericEventArgs<byte[]>> handler = Received;
      if (handler != null) Received(this, new GenericEventArgs<byte[]>(pData));
    }

    public virtual ILogger Logger {
      get { return mLogger; }
      set { mLogger = value; }
    }

    #region Dispose

    private bool mDisposed = false;

    public void Dispose() {
      if (mDisposed) return;

      mDisposed = true;

      Close();
    }

    ~UDPBroadcastListener() {
      Dispose();
    }

    #endregion

  }
}

I am now getting extremely desperate. Please help. :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-08-21 16:54:15

您可以尝试使用 TCPView (它也显示 UDP)进行调查查看生产服务器上是否显示任何内容。查看“Windows 防火墙设置疑难解答”,尽管这是针对 XP 的,我怀疑您的生产服务器是XP,但无论如何它可能会有帮助。请参阅 netstat 示例。

You could try investigating with TCPView (It shows UDP as well) to see if anything shows up on the production server. Check out "Troubleshooting Windows Firewall settings", although this is for XP and I doubt your production server is XP, but it may be helpful anyway. See the netstat example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文