Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NET Compact framework中的异步TCP服务器_C#_Sockets_Tcp_Compact Framework - Fatal编程技术网

C# NET Compact framework中的异步TCP服务器

C# NET Compact framework中的异步TCP服务器,c#,sockets,tcp,compact-framework,C#,Sockets,Tcp,Compact Framework,我正在.NETCompactFramework3.5(控制台应用程序)中编写异步TCP服务器,我仅将套接字类用作不支持的TcpListener.netCF。我使用了msdn()中给出的相同服务器代码。但在我的系统挂起2/3天后,我的系统是带有64MB RAM的ARM设备。可能的原因是什么。我在同步TCP服务器中没有遇到这样的问题。 这是我的密码 public void StartListening() { try { IPH

我正在.NETCompactFramework3.5(控制台应用程序)中编写异步TCP服务器,我仅将套接字类用作不支持的TcpListener.netCF。我使用了msdn()中给出的相同服务器代码。但在我的系统挂起2/3天后,我的系统是带有64MB RAM的ARM设备。可能的原因是什么。我在同步TCP服务器中没有遇到这样的问题。 这是我的密码

    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);
        }
    }
public-void-listening()
{
尝试
{
IPHostEntry ipHostInfo=Dns.Resolve(Dns.GetHostName());
IPAddress IPAddress=ipHostInfo.AddressList[0];
IPEndPoint localEndPoint=新IPEndPoint(ipAddress,8001);
//创建TCP/IP套接字。
套接字侦听器=新套接字(AddressFamily.InterNetwork、SocketType.Stream、ProtocolType.Tcp);
//将套接字绑定到本地端点并侦听传入连接。
Bind(localEndPoint);
听,听(4);
while(true)
{
尝试
{
//启动异步套接字以侦听连接。
beginacept(新的异步回调(AcceptCallback),listener);
//等待连接完成后再继续。
全部完成。WaitOne();
}
捕获(异常pEx)
{
_serverLog.WriteFile(pEx);
}
}
}
捕获(异常pEx)
{
_serverLog.WriteFile(pEx);
}
}
/// 
/// 
/// 
/// 
公共无效接受回调(IAsyncResult ar)
{
allDone.Set();
套接字侦听器=(套接字)ar.AsyncState;
套接字处理程序=listener.EndAccept(ar);
//beginacept(新的异步回调(AcceptCallback),listener);
StateObject状态=新的StateObject();
state.workSocket=处理程序;
BeginReceive(state.buffer,0,StateObject.BufferSize,0,新异步回调(ReadCallback),state);
//beginacept(新的异步回调(AcceptCallback),listener);
}
/// 
/// 
/// 
公共void ReadCallback(IAsyncResult ar)
{
StateObject状态=(StateObject)ar.AsyncState;
套接字处理程序=state.workSocket;
字符串localIP=handler.RemoteEndPoint.ToString();
var_port=localIP.Split(':');
字符串ip=port[0]+”:“+port[1];
int bytesRead=handler.EndReceive(ar);
如果(字节读取>0)
{
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));
_receivedData.Enqueue(state.sb.ToString());
发送(处理程序,“-ACK-接收数据”);
//QueueUserWorkItem(新的WaitCallback(ProcessData))\u ip;
这个.ProcessData(_ip);
}
}
/// 
/// 
/// 
/// 
/// 
私有void发送(套接字处理程序、字符串数据)
{
//使用ASCII编码将字符串数据转换为字节数据。
byte[]byteData=Encoding.ASCII.GetBytes(数据);
//开始将数据发送到远程设备。
BeginSend(byteData,0,byteData.Length,0,新异步回调(SendCallback),handler);
}
/// 
/// 
/// 
/// 
私有void SendCallback(IAsyncResult ar)
{
尝试
{
//从状态对象检索套接字。
套接字处理程序=(套接字)ar.AsyncState;
//完成将数据发送到远程设备。
int bytesSent=handler.EndSend(ar);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
捕获(异常pEx)
{
_serverLog.WriteFile(pEx);
}
}

可能是您没有在此处发布的部分,但我在任何地方都看不到您清空StringBuffer
state.sb
或队列
\u receivedData
(仅假设按名称/方法的类型)的代码。填充这两个资源可能会导致系统挂起,因为在某个点上没有更多内存…

嗨,罗兰,我正在用另一种方法退出\u receivedData队列。每次我创建新的StateObject时,StringBuffer state.sb都会设置为null。也许你可以更具体地描述一下“系统挂起”:整个设备是否挂起,应用程序是否被阻止,它是否停止接受连接请求,或者症状是什么?您是否跟踪了应用程序/系统的内存消耗/cpu负载等?嗨,罗兰,系统挂起意味着只有应用程序会被阻塞,而不是操作系统。是的,它将停止接受连接。我们跟踪了内存使用情况,它逐渐增加,CPU负载也随之增加。