3

私のアプリケーションはウィンドウのシャットダウンを防ぎますが、一部のコンピューターでのみ、常にではありません。デバッグするのは少し難しいです。TCPサーバーが原因だと思います。これは非同期サーバーであり、私のアプリケーションはCloseReason==WindowsShutDownを処理します。これが発生した場合、アプリケーションはまだプロセスとして実行されていますが、タスクバー/システムトレイからアクセスできません。

誰かが私のサーバーコードに関する明らかな問題を見ることができるかどうか疑問に思いました。

以下は私のサーバーのコードです。Stop()メソッドは、メインフォームのClose()イベントから呼び出されます。

public class MantraServer
    {
        protected int portNumber;
        private bool ShuttingDown = false;

        //the main socket the server listens to
        Socket listener;

        //Constructor - Start a server on the given IP/port
        public MantraServer(int port, IPAddress IP)
        {
            this.portNumber = port;
            Start(IP);
        }

        /// 
        /// Description: Start the threads to listen to the port and process
        /// messages.
        ///
        public void Start(IPAddress IP)
        {
            try
            {
                //We are using TCP sockets
                listener = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Stream,
                                          ProtocolType.Tcp);

                //Assign the any IP of the machine and listen on port number 3000
                IPEndPoint ipEndPoint = new IPEndPoint(IP, 3000);

                //Bind and listen on the given address
                listener.Bind(ipEndPoint);
                listener.Listen(10);

                //Accept the incoming clients
                listener.BeginAccept(new AsyncCallback(OnAccept), listener);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MANTRA Network Start Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// Decription: Stop the threads for the port listener.
        public bool Stop()
        {
            try
            {
                ShuttingDown = true;
                listener.Shutdown(SocketShutdown.Both);
                listener.Close();
                listener = null;
                System.Threading.Thread.Sleep(500); //wait for half second while the server closes
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// 
        /// Decription: Call back method to accept new connections.
        /// <param name="ar">Status of an asynchronous operation.</param>
        private void OnAccept(IAsyncResult ar)
        {
            try
            {
                if (!ShuttingDown)
                {
                    MantraStatusMessage InMsg = new MantraStatusMessage();
                    InMsg.Socket = ((Socket)ar.AsyncState).EndAccept(ar);
                    //Start listening for more clients
                    listener.BeginAccept(new AsyncCallback(OnAccept), listener);

                    //Once the client connects then start receiving the commands from them
                    InMsg.Socket.BeginReceive(InMsg.buffer, 0, InMsg.buffer.Length, SocketFlags.None,
                        new AsyncCallback(OnReceive), InMsg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "MANTRA Network Accept Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        ///  
        /// Receives the data, puts it in a buffer and checks if we need to receive again.  
        public void OnReceive(IAsyncResult result)
        {
            MantraStatusMessage InMsg = (MantraStatusMessage)result.AsyncState;
            int read = InMsg.Socket.EndReceive(result);
            if (read > 0)
            {
                for (int i = 0; i < read; i++)
                {
                    InMsg.TransmissionBuffer.Add(InMsg.buffer[i]);
                }
                //we need to read again if this is true  
                if (read == InMsg.buffer.Length)
                {
                    InMsg.Socket.BeginReceive(InMsg.buffer, 0, InMsg.buffer.Length, SocketFlags.None, OnReceive, InMsg);
                    Console.Out.WriteLine("Message Too big!");
                }
                else
                {
                    Done(InMsg);
                }
            }
            else
            {
                Done(InMsg);
            }
        }

        ///  
        /// Deserializes and outputs the received object  
        public void Done(MantraStatusMessage InMsg)
        {
            Console.Out.WriteLine("Received: " + InMsg.msg);
            MantraStatusMessage received = InMsg.DeSerialize();
            Console.WriteLine(received.msg.Message);
        }
    }

編集

Hoganのおかげで、Close()の呼び出しに関する詳細情報は次のとおりです。

ソケットが接続されておらず、(sendto呼び出しを使用してデータグラムソケットで送信する場合)アドレスが指定されていないため、データの送受信要求は許可されませんでした。

これが何を意味するのかはまだ完全にはわかりません。

4

2 に答える 2

1

何が起こっているかを確認するには、Windowsイベントログにログを追加する必要があります。

開始するのに最適な場所は、falseを返すキャッチです(これにより、ウィンドウのシャットダウンが停止します)。そこで理由をログに記録すると、少なくともイベントログを調べて、サービスがシャットダウンしない理由を確認できます。

于 2011-04-29T01:18:08.933 に答える
1

コールバックが発生したときは、非同期メソッドのEndXXX対応メソッドを必ず呼び出す必要があります。あなたはこれをするのに失敗します:

InMsg.Socket = ((Socket)ar.AsyncState).EndAccept(ar);

!shuttingDownそれはブロックに住んでいるからです。それを呼んでください...エラーをキャッチします。

于 2011-04-29T01:20:06.603 に答える