在套接字编程中,exe文件内存逐渐增加,socket也保持打开状态

时间:2018-06-12 11:25:13

标签: sockets websocket tcpclient tcplistener

当从多个客户端收到sting时,某个套接字保持打开,因此创建的文件大小逐渐增加,所以在exe文件从35 kb变为2 GB时,如何减少开放套接字

private void Server_Load(object sender, EventArgs e)
{
  try
   {
        this.tcpListener = new TcpListener(IPAddress.Any, port);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
   }
  catch (Exception ex)
  { }
  finally
  {
    if (this.tcpListener != null)
    {
        this.tcpListener.Stop();
    }
  }
} 

服务器连续从负载方法请求Mangae客户端

private void ListenForClients()
{
 TcpClient client = null;
 try
 {
    this.tcpListener.Start();
    while (true)
    {
         client = this.tcpListener.AcceptTcpClient();
        ThreadPool.QueueUserWorkItem(new WaitCallback(HandleClientComm), 
     client);
    }
 }
 catch (Exception ex)
 {
    LogHelperISPL.Logger.Info("ListenForClients: " + ex.Message);
    this.tcpListener.Stop();
 }
 finally
 {
    if(this.tcpListener != null)
    {
        this.tcpListener.Stop();
    }
    if (client != null)
    {
        client.Close();
    }
 }
}

从客户端获取数据并插入到表中并通过密切连接管理tcpclient和networkstream

private void HandleClientComm(object client)
{
 TcpClient tcpClient = null;
 NetworkStream clientStream = null;
 try
 {
    tcpClient = (TcpClient)client;
    clientStream = tcpClient.GetStream();
    string InsertedRecord = string.Empty;
    byte[] messageBytes = new byte[4096];
    int bytesRead;
    bool end = false;
    while (!end)
    {
        bytesRead = 0;
        try
        {
            if (clientStream != null)
            {
                bytesRead = clientStream.Read(messageBytes, 0, 
            messageBytes.Length);
            }                        
        }
        catch (SocketException ex)
        {
            if (clientStream != null)
            {
                clientStream.Flush();
                clientStream.Close();
            }
            if (tcpClient != null)
            {
                tcpClient.Close();
            }
            break;
        }
        catch (Exception ex)
        {
            if (clientStream != null)
            {
                clientStream.Flush();
                clientStream.Close();
            }
            if (tcpClient != null)
            {
                tcpClient.Close();
            }
            break;
        }
        if (bytesRead <= 0)
        {
            break;
        }
        ASCIIEncoding encoder = new ASCIIEncoding();
        string Datareceived = encoder.GetString(messageBytes, 0, bytesRead);
        if (!string.IsNullOrEmpty(Datareceived))
        {
            string[] Multistrings = Datareceived.Split('!');
            for (int i = 0; i < Multistrings.Length; i++)
            {
                if (!string.IsNullOrEmpty(Multistrings[i]))
                {
                    if (Multistrings[i].Length >= 90)
                    {
                        InsertedRecord = InsertRawData(Multistrings[i]);
                    }
                    else
                    {
                        InsertedRecord = 
       InsertRawDataGarbage(Multistrings[i]);
                    }
                }
            }
        }
    }
 }
 catch (Exception ex)
 {
    LogHelperISPL.Logger.Info("While loop: " + ex.Message);
 }
 finally
 {
    if (clientStream != null)
    {
        clientStream.Flush();
        clientStream.Close();
    }
    if (tcpClient != null)
    {
        tcpClient.Close();
    }
 }
}

0 个答案:

没有答案