Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. I have a "High CPU usage" problem! Help me please!

I have a "High CPU usage" problem! Help me please!

Scheduled Pinned Locked Moved C#
helpsysadmin
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rojan Gh
    wrote on last edited by
    #1

    Hi everyone. I have a coded a Proxy Server like code that works really fine for me! But there is an stupid problem! When I begin surfing the internet through my program (using the proxy setting in firefox) at the beginning, the CPU usage is normal with some times raising up to 2 and then back to 0 again, but as soon as I open 2 or 3 pages in firefox, specially the page with flv videos, the CPU usage raises up to 99 percent! Here is my TCP process code: private void ClientConnectionHandler(object client) { TcpClient tcpClient = null; NetworkStream networkStream = null; TcpClient remoteTcpClient = null; NetworkStream remoteNetworkStream = null; try { tcpClient = (TcpClient)client; const int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; tcpClient.ReceiveBufferSize = bufferSize; tcpClient.SendBufferSize = bufferSize; networkStream = tcpClient.GetStream(); int bytesRead = networkStream.Read(buffer, 0, bufferSize); string request = Encoding.ASCII.GetString(buffer, 0, bytesRead); string host = ""; string[] headers = request.Split(new string[] { "\r\n" }, StringSplitOptions.None); foreach (string header in headers) { if (header.ToLower().Replace(" ", "").Contains("host:")) { host = header.Replace(" ", "").Split(':')[1]; } } IPAddress[] hostAddresses = Dns.GetHostAddresses(host); remoteTcpClient = new TcpClient { ReceiveBufferSize = bufferSize, SendBufferSize = bufferSize }; remoteTcpClient.Connect(hostAddresses[0].ToString(), 80); remoteNetworkStream = remoteTcpClient.GetStream(); remoteNetworkStream.Write(buffer, 0, bytesRead); bool eof = request.Contains("\r\n"); while (!eof) { bytesRead = networkStream.Read(buffer, 0, bufferSize); remoteNetworkStream.Write(buffer, 0, bytesRead); eof = Encoding.ASCII.GetString(buffer, 0, byte

    R 1 Reply Last reply
    0
    • R Rojan Gh

      Hi everyone. I have a coded a Proxy Server like code that works really fine for me! But there is an stupid problem! When I begin surfing the internet through my program (using the proxy setting in firefox) at the beginning, the CPU usage is normal with some times raising up to 2 and then back to 0 again, but as soon as I open 2 or 3 pages in firefox, specially the page with flv videos, the CPU usage raises up to 99 percent! Here is my TCP process code: private void ClientConnectionHandler(object client) { TcpClient tcpClient = null; NetworkStream networkStream = null; TcpClient remoteTcpClient = null; NetworkStream remoteNetworkStream = null; try { tcpClient = (TcpClient)client; const int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; tcpClient.ReceiveBufferSize = bufferSize; tcpClient.SendBufferSize = bufferSize; networkStream = tcpClient.GetStream(); int bytesRead = networkStream.Read(buffer, 0, bufferSize); string request = Encoding.ASCII.GetString(buffer, 0, bytesRead); string host = ""; string[] headers = request.Split(new string[] { "\r\n" }, StringSplitOptions.None); foreach (string header in headers) { if (header.ToLower().Replace(" ", "").Contains("host:")) { host = header.Replace(" ", "").Split(':')[1]; } } IPAddress[] hostAddresses = Dns.GetHostAddresses(host); remoteTcpClient = new TcpClient { ReceiveBufferSize = bufferSize, SendBufferSize = bufferSize }; remoteTcpClient.Connect(hostAddresses[0].ToString(), 80); remoteNetworkStream = remoteTcpClient.GetStream(); remoteNetworkStream.Write(buffer, 0, bytesRead); bool eof = request.Contains("\r\n"); while (!eof) { bytesRead = networkStream.Read(buffer, 0, bufferSize); remoteNetworkStream.Write(buffer, 0, bytesRead); eof = Encoding.ASCII.GetString(buffer, 0, byte

      R Offline
      R Offline
      Rutvik Dave
      wrote on last edited by
      #2

      Try to use bigger bufferSize, so that it takes less itration. or Try to create a seperate thread to receive the data, Thread are lightweight then process. so it might restrict the cpu usage.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups