ASP.NET UDP Socket Code Works in Design, but Not in Production on IIS

I have the following UDP broadcast listener that works as a static component in a separate thread in an ASP.NET web application. Why I would do it really doesn’t matter, but the reason it won’t work when they deploy me confuses me. I have several console applications that send UDP broadcasts, and I encoded the tested one and confirmed this stream and its code working while working in Visual Studio on the VS2005 development web server, but at the moment when I compile the code and deploy it to to another machine and run it under IIS - it stops working. Also, it does not do ERROR, it just does not work.

The log4net log also works only in the Init () method, but not in the stream (which is strange). But I added a few File.AppendAllTextto the stream, and it seems to execute, but as far as I can tell, it stops at the linebyte[] received = mUdpClient.Receive(ref mGroupEP);

I made sure that the port is open and tested in the console application (which works), I checked the permissions, even made the default identifier using the Local System user account, but still nothing. I checked the firewall, checked the event log - nothing. No errors, just not working.

I wondered if there could be something to do with IIS, closing System.Net requests as a security measure, but I cannot find anything that could support this 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

  }
}

Now I am becoming extremely desperate. Please help.: (

+3
1

TCPView ( UDP), , - . Windows", XP, , XP, . . netstat.

+2

Source: https://habr.com/ru/post/1725674/


All Articles