ASP.NET UDP套接字代码可在开发中使用,但不能在IIS上的生产中使用 [英] ASP.NET UDP socket code works in development, but not in production on IIS

查看:70
本文介绍了ASP.NET UDP套接字代码可在开发中使用,但不能在IIS上的生产中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

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. :(

推荐答案

您可以尝试使用对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.

这篇关于ASP.NET UDP套接字代码可在开发中使用,但不能在IIS上的生产中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆