2

私は.netコンパクトフレームワーク3.5(コンソールアプリ)で非同期TCPサーバーを書いています.TcpListenerが.netCFをサポートしていないため、ソケットクラスのみを使用しました。msdn ( http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.90).aspx ) で指定されているものと同じサーバー コードを使用しました。しかし、2/3 日後にシステムがハングします。私のシステムは 64 MB RAM の ARM デバイスです。考えられる原因は何ですか。Synchronous TCP Server ではこのような問題は発生しませんでした。これが私のコードです。

    public void StartListening()
    {
        try
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8001);

            // Create a TCP/IP socket.
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            listener.Bind(localEndPoint);
            listener.Listen(4);

            while (true)
            {
                try
                {
                    // Start an asynchronous socket to listen for connections.
                    listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

                    // Wait until a connection is made before continuing.
                    allDone.WaitOne();
                }
                catch (Exception pEx)
                {
                    _serverLog.WriteFile(pEx);
                }
            }
        }
        catch (Exception pEx)
        {
            _serverLog.WriteFile(pEx);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ar"></param>
    public void AcceptCallback(IAsyncResult ar)
    {
        allDone.Set();

        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        //listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);

        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);

        //listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
    }

    /// <summary>
    /// 
    /// </summary>
    public void ReadCallback(IAsyncResult ar)
    {
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        string localIP = handler.RemoteEndPoint.ToString();
        var _port = localIP.Split(':');
        string _ip = _port[0] + ":" + _port[1];

        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
            _receivedData.Enqueue(state.sb.ToString());

            Send(handler, " - ACK - Data Received.");

            //ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessData), _ip);
            this.ProcessData(_ip);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="handler"></param>
    /// <param name="data"></param>
    private void Send(Socket handler, String data)
    {
        // Convert the string data to byte data using ASCII encoding.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // Begin sending the data to the remote device.
        handler.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), handler);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="ar"></param>
    private void SendCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket handler = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = handler.EndSend(ar);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();
        }
        catch (Exception pEx)
        {
            _serverLog.WriteFile(pEx);
        }
    }
4

1 に答える 1

1

Maybe it is in a part you haven't posted here, but I can't see anywhere some code where you empty the StringBuffer state.sb or the Queue _receivedData (just assuming the types by Name/Method). Filling this two ressources can bring your system to hang as at some point there is no more memory...

于 2013-09-04T09:35:26.277 に答える