Click here to Skip to main content
15,912,837 members
Home / Discussions / C#
   

C#

 
AnswerRe: First programming project what are my choices for storing data. Pin
PIEBALDconsult25-Sep-09 12:13
mvePIEBALDconsult25-Sep-09 12:13 
AnswerRe: First programming project what are my choices for storing data. Pin
Christian Graus25-Sep-09 12:35
protectorChristian Graus25-Sep-09 12:35 
AnswerRe: First programming project what are my choices for storing data. Pin
Alan Balkany28-Sep-09 5:05
Alan Balkany28-Sep-09 5:05 
GeneralRe: First programming project what are my choices for storing data. Pin
PROPZ_7628-Sep-09 12:24
PROPZ_7628-Sep-09 12:24 
QuestionTake a databse back up with LINQ ? Pin
Mohammad Dayyan25-Sep-09 10:30
Mohammad Dayyan25-Sep-09 10:30 
AnswerRe: Take a databse back up qith LINQ ? Pin
Richard MacCutchan25-Sep-09 10:51
mveRichard MacCutchan25-Sep-09 10:51 
Questionabout C# books Pin
CoderForEver25-Sep-09 9:51
CoderForEver25-Sep-09 9:51 
AnswerRe: about C# books Pin
Pete O'Hanlon25-Sep-09 11:22
mvePete O'Hanlon25-Sep-09 11:22 
GeneralRe: about C# books Pin
CoderForEver25-Sep-09 23:50
CoderForEver25-Sep-09 23:50 
GeneralRe: about C# books Pin
Richard MacCutchan26-Sep-09 0:00
mveRichard MacCutchan26-Sep-09 0:00 
GeneralRe: about C# books Pin
CoderForEver26-Sep-09 0:20
CoderForEver26-Sep-09 0:20 
GeneralRe: about C# books Pin
Richard MacCutchan26-Sep-09 0:34
mveRichard MacCutchan26-Sep-09 0:34 
GeneralRe: about C# books Pin
CoderForEver26-Sep-09 0:46
CoderForEver26-Sep-09 0:46 
QuestionC# Syntax/Programming Theory Question Pin
Xpnctoc25-Sep-09 8:40
Xpnctoc25-Sep-09 8:40 
AnswerRe: C# Syntax/Programming Theory Question Pin
PIEBALDconsult25-Sep-09 8:51
mvePIEBALDconsult25-Sep-09 8:51 
GeneralRe: C# Syntax/Programming Theory Question Pin
Xpnctoc25-Sep-09 9:11
Xpnctoc25-Sep-09 9:11 
AnswerRe: C# Syntax/Programming Theory Question Pin
Henry Minute25-Sep-09 9:47
Henry Minute25-Sep-09 9:47 
AnswerRe: C# Syntax/Programming Theory Question Pin
Pete O'Hanlon25-Sep-09 11:14
mvePete O'Hanlon25-Sep-09 11:14 
GeneralRe: C# Syntax/Programming Theory Question Pin
Xpnctoc25-Sep-09 11:17
Xpnctoc25-Sep-09 11:17 
GeneralRe: C# Syntax/Programming Theory Question Pin
PIEBALDconsult25-Sep-09 12:23
mvePIEBALDconsult25-Sep-09 12:23 
AnswerRe: C# Syntax/Programming Theory Question Pin
Alan Balkany28-Sep-09 5:15
Alan Balkany28-Sep-09 5:15 
QuestionI have a "High CPU usage" problem! Help me please! Pin
Rojan Gh.25-Sep-09 7:30
professionalRojan Gh.25-Sep-09 7:30 
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:

<br />
        private void ClientConnectionHandler(object client)<br />
        {            <br />
            TcpClient tcpClient = null;<br />
<br />
            NetworkStream networkStream = null;<br />
<br />
            TcpClient remoteTcpClient = null;<br />
<br />
            NetworkStream remoteNetworkStream = null;<br />
<br />
            try<br />
            {<br />
                tcpClient = (TcpClient)client;<br />
<br />
                const int bufferSize = 1024;<br />
<br />
                byte[] buffer = new byte[bufferSize];<br />
<br />
                tcpClient.ReceiveBufferSize = bufferSize;<br />
<br />
                tcpClient.SendBufferSize = bufferSize;<br />
<br />
                networkStream = tcpClient.GetStream();<br />
<br />
                int bytesRead = networkStream.Read(buffer, 0, bufferSize);<br />
<br />
                string request = Encoding.ASCII.GetString(buffer, 0, bytesRead);<br />
<br />
                string host = "";<br />
<br />
                string[] headers = request.Split(new string[] { "\r\n" }, StringSplitOptions.None);<br />
<br />
                foreach (string header in headers)<br />
                {<br />
                    if (header.ToLower().Replace(" ", "").Contains("host:"))<br />
                    {<br />
                        host = header.Replace(" ", "").Split(':')[1];<br />
                    }<br />
                }<br />
<br />
                IPAddress[] hostAddresses = Dns.GetHostAddresses(host);<br />
<br />
                remoteTcpClient = new TcpClient<br />
                {<br />
                    ReceiveBufferSize = bufferSize,<br />
                    SendBufferSize = bufferSize<br />
                };<br />
<br />
                remoteTcpClient.Connect(hostAddresses[0].ToString(), 80);<br />
<br />
                remoteNetworkStream = remoteTcpClient.GetStream();<br />
<br />
                remoteNetworkStream.Write(buffer, 0, bytesRead);<br />
<br />
                bool eof =<br />
                    request.Contains("\r\n");<br />
<br />
                while (!eof)<br />
                {<br />
                    bytesRead = networkStream.Read(buffer, 0, bufferSize);<br />
<br />
                    remoteNetworkStream.Write(buffer, 0, bytesRead);<br />
<br />
                    eof =<br />
                        Encoding.ASCII.GetString(buffer, 0, bytesRead).Contains("\r\n");<br />
                }<br />
<br />
                bytesRead = remoteNetworkStream.Read(buffer, 0, bufferSize);<br />
<br />
                int received = bytesRead;<br />
<br />
                networkStream.Write(buffer, 0, bytesRead);<br />
<br />
                string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);<br />
<br />
                int contentLength = 0;<br />
<br />
                headers = response.Split(new string[] { "\r\n" }, StringSplitOptions.None);<br />
<br />
                foreach (string header in headers)<br />
                {<br />
                    if (header.ToLower().Replace(" ", "").Contains("content-length:"))<br />
                    {<br />
                        contentLength = int.Parse(header.Replace(" ", "").Split(':')[1]);<br />
                    }<br />
                }<br />
<br />
                int headerLength = response.IndexOf("\r\n\r\n") + 4;<br />
<br />
                int totalLength = headerLength + contentLength;<br />
<br />
                eof = (received == totalLength);<br />
<br />
                while (!eof)<br />
                {<br />
                    bytesRead = remoteNetworkStream.Read(buffer, 0, bufferSize);<br />
<br />
                    BytesRecieve += bytesRead;<br />
<br />
                    received += bytesRead;<br />
<br />
                    networkStream.Write(buffer, 0, bytesRead);<br />
<br />
                    eof =<br />
                        (received == totalLength);<br />
                }<br />
            }<br />
            catch (Exception e)<br />
            {<br />
                Console.WriteLine(e.Message + "\r\n\r\n");<br />
            }<br />
            finally<br />
            {<br />
                if (tcpClient != null) tcpClient.Close();<br />
<br />
                if (networkStream != null)<br />
                {<br />
                    networkStream.Close();<br />
<br />
                    networkStream.Dispose();<br />
                }<br />
<br />
                if (remoteTcpClient != null) remoteTcpClient.Close();<br />
<br />
                if (remoteNetworkStream != null)<br />
                {<br />
                    remoteNetworkStream.Close();<br />
<br />
                    remoteNetworkStream.Dispose();<br />
                }<br />
            }<br />
<br />
            Thread.CurrentThread.Abort();<br />
}<br />


I start the TcpListener usign this method:

<br />
private void StartServer()<br />
        {<br />
            var tcpListener = new TcpListener("127.0.0.1",777);<br />
<br />
            tcpListener.Start();<br />
<br />
            ThreadPool.SetMaxThreads(5, 5);<br />
<br />
            while (AcceptConnection)<br />
            {<br />
                TcpClient tcpClient = tcpListener.AcceptTcpClient();<br />
<br />
                ThreadPool.QueueUserWorkItem(ClientConnectionHandler, tcpClient);<br />
            }<br />
        }<br />


And also, I used these in a console program.

Does anyone know the problem please?

Sojaner!

AnswerRe: I have a "High CPU usage" problem! Help me please! Pin
Rutvik Dave25-Sep-09 11:54
professionalRutvik Dave25-Sep-09 11:54 
QuestionCalendar DayView help Pin
An Enigma25-Sep-09 7:27
An Enigma25-Sep-09 7:27 
AnswerRe: Calendar DayView help Pin
Christian Graus25-Sep-09 10:57
protectorChristian Graus25-Sep-09 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.