增加套接字客户端的内存使用量

时间:2014-12-31 11:58:05

标签: c# sockets asynchronous console-application asyncsocket

我正在尝试开发一个在C#中充当异步套接字客户端的控制台应用程序。您可以看到以下代码:

public class StateObject
{
    // Client socket.
    public Socket workSocket = null;
    // Size of receive buffer.
    public const int BufferSize = 1024;
    // Receive buffer.
    public byte[] buffer = new byte[BufferSize];
    // Received data string.
    public StringBuilder sb = new StringBuilder();
}

class Program
{
    private static readonly string hostIp = ConfigurationManager.AppSettings["HostIp"];
    private static readonly int port = Int32.Parse(ConfigurationManager.AppSettings["HostPort"]);
    private static Socket client; 
    private static ManualResetEvent connectDone = new ManualResetEvent(false);
    private static ManualResetEvent sendDone = new ManualResetEvent(false);
    private static ManualResetEvent receiveDone = new ManualResetEvent(false);
    private static Thread receiveThread;

    static int Main(string[] args)
    {
        EventLog appLog = new EventLog();
        appLog.Source = "xApp";

        try
        {
            IPHostEntry ipHostInfo = Dns.GetHostEntry(hostIp);
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

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

            // Connect to the remote endpoint.
            client.BeginConnect(remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            // Send test data to the remote device.
            Send(client, "Login Message");
            sendDone.WaitOne();

            receiveThread = new Thread((ThreadStart)delegate
            {
                while (true)
                {
                    Receive(client);
                    receiveDone.WaitOne();
                    Thread.Sleep(1);
                }
            });
            receiveThread.Start();
        }
        catch (Exception ex)
        {
            appLog.WriteEntry(
                "An exception occured: " +
                " ex: " + ex.ToString() +
                " stack trace: " + ex.StackTrace,
                System.Diagnostics.EventLogEntryType.Error);
        }

        return 0;
    }

    private static void Receive(Socket client)
    {
        try
        {
            // Create the state object.
            StateObject state = new StateObject();
            state.workSocket = client;

            // Begin receiving the data from the remote device.
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReceiveCallback), state);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            StateObject state = (StateObject)ar.AsyncState;
            Socket client = state.workSocket;

            // Read data from the remote device.
            int bytesRead = client.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                Console.WriteLine("Response received : {0}", state.sb.ToString());
                string[] args = state.sb.ToString().Split(';');
                switch (args[1])  
                {
                    case "CREATEBOOK":
                        ProcessInput(args);
                        break;
                    case "CONFIRMBOOK":
                        if (args[2] == "true")   
                        {
                            ConfirmProcess();
                        }
                        break;
                    default:
                        break;
                }

                receiveDone.Set();
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void Send(Socket client, String data)
    {
        byte[] byteData = Encoding.Unicode.GetBytes(data);

        client.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), client);
    }

    private static void SendCallback(IAsyncResult ar)
    {
        try
        {
            Socket client = (Socket)ar.AsyncState;

            // Complete sending the data to the remote device.
            int bytesSent = client.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to server.", bytesSent);

            // Signal that all bytes have been sent.
            sendDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

    private static void ConnectCallback(IAsyncResult ar)
    {
        try
        {
            // Retrieve the socket from the state object.
            Socket client = (Socket)ar.AsyncState;

            // Complete the connection.
            client.EndConnect(ar);

            Console.WriteLine("Socket connected to {0}",
                client.RemoteEndPoint.ToString());

            // Signal that the connection has been made.
            connectDone.Set();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }
}

当我调试时,我发现代码按预期工作,但进程使用的内存大小每时每刻都在增加。我认为内存泄漏的原因如下:

receiveThread = new Thread((ThreadStart)delegate
{
    while (true)
    {
        Receive(client);
        receiveDone.WaitOne();
        Thread.Sleep(1);
    }
});
receiveThread.Start();

但我对我不得不做的改变一无所知。你有任何提示吗?

提前致谢,

1 个答案:

答案 0 :(得分:1)

我认为问题出在您在Receive循环中调用的while方法中。基本上你每次循环时都会创建一个新的StateObject

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

尝试将状态对象存储为类变量并重用它。如果您需要再次重新初始化,可以添加Reset方法。 This article显示了一种构建非常有效的异步套接字的方法,您可能会发现它很有用。