简体   繁体   English

.NET Compact框架中的异步TCP服务器

[英]Async TCP Server in .NET Compact framework

I'm writing asynchronous TCP Server in .net compact framework 3.5(console app), i used socket class only as TcpListener not supported .netCF. 我在.net Compact Framework 3.5(控制台应用程序)中编写异步TCP Server,我仅使用套接字类,因为TcpListener不支持.netCF。 i used the same server code as given in msdn ( http://msdn.microsoft.com/en-us/library/fx6588te(v=vs.90).aspx ). 我使用了与msdn( http://msdn.microsoft.com/zh-cn/library/fx6588te (v=vs.90) .aspx )中给出的相同的服务器代码。 but after 2/3 days my system hangs, my system is ARM device with 64 MB RAM. 但是在系统挂起2/3天后,我的系统是具有64 MB RAM的ARM设备。 what may be the possible cause. 可能是什么原因。 I didn't get such problem in Synchronous TCP Server. 我在同步TCP服务器中没有得到这样的问题。 Here is my code. 这是我的代码。

    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);
        }
    }

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). 也许这是您尚未在此处发布的一部分,但是我看不到任何代码可以在其中清空StringBuffer state.sb或Queue _receivedData (仅通过Name / Method假定类型)。 Filling this two ressources can bring your system to hang as at some point there is no more memory... 填充这两个资源可以使您的系统挂起,因为在某个时候没有更多的内存...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM