简体   繁体   中英

TCP server in C# causing 100% CPU usage after 2-3 days

Here's my code for a very simple TCP Server (basically the sample Asynchronous Server Socket example - http://goo.gl/Ix5C - modified slightly):

    public static void InitiateListener()
    {
        try
        {
            allDone = new ManualResetEvent(false);
            configFile = new XmlConfig();                
            StartListening();
        }
        catch (Exception exc)
        {
            LogsWriter f = new LogsWriter(configFile.ErrorLogsPath);
            f.WriteToFile(exc.Message);
            f.CloseFile();
        }

    }

    private static void StartListening()
    {      
        try
        {
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, configFile.Port);
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            listener.Bind(localEndPoint);
            listener.Listen(100);                

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();                    

                // 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 exc)
        {
            throw exc;
        }
    }

    public static void AcceptCallback(IAsyncResult ar)
    {
        allDone.Set(); // Signal the main thread to continue.               

        // Get the socket that handles the client request.
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // Create the state object.
        StateObject state = new StateObject();
        state.workSocket = handler;

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

    public static void ReadCallback(IAsyncResult ar)
    {
        string hexData = string.Empty;

        // Retrieve the state object and the handler socket from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        try
        {
            // Read data from the client socket. 
            int bytesRead = handler.EndReceive(ar);                

            if (bytesRead > 0)
            {
                hexData = BitConverter.ToString(state.buffer);

                if (hexData.Contains("FA-F8")) //heartbeat - echo the data back to the client.
                {
                    byte[] byteData = state.buffer.Take(bytesRead).ToArray();
                    handler.Send(byteData);
                }
                else if (hexData.Contains("0D-0A")) //message
                {
                    state.AsciiData = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
                    state.ParseMessage(configFile);
                }
            }
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);                
        }
        catch (Exception)
        {
            handler.Shutdown(SocketShutdown.Both);
            handler.Close();
        }
    }

This is all in a Windows service. And the CPU maxes to 100% after about 2 and a half days of running perfectly acceptably. This has happened 3 times now - the windows service always works fine and functions as it's supposed to, utlizing almost no CPU resources but sometime on the 3rd day goes to 100% and stays there until the service is resarted.

I get very simple CSV packets, that I parse quickly and send it off to a database via a webservice in this method: state.ParseMessage(configFile); Even when the CPU is 100%, the database gets filled up pretty reliably. But I understand this could be one place where I need to investigate?

Which other areas of the code seem like they could be causing the problem? I'm new to async programming, so I don't know if I need to close threads manually. Also, this might be another problem: handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state);
Calling that line within ReadCallback itself. I do this to maintain the connection with the client and continue to receive the data, but maybe I should close to the socket and force a new connection?

Could you please offer some suggestions? Thanks

Instead of loop while(true) in startlistening method we need to call

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

from AcceptCallback method once completion of accepting new client.

You need to detect disconnection through bytesRead == 0 . Right now you don't and instead issue another read.

Looks like you have copied MSDN sample code. I can tell because of the insightless combination of async IO and events. All this code becomes simpler when you use synchronous IO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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