Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There should be 2 break statements in this infinite while loop

Tags:

c#

This is an example from Microsoft socket tutorial http://msdn.microsoft.com/en-us/library/6y0e13d3.aspx

I am a bit confused. The first while(true) infinite loop is followed by a second one 4 lines down, yet we only use one break statement. Using break in the second while loop should continue the the first while loop... no? http://msdn.microsoft.com/en-us/library/6y0e13d3.aspx

while (true) {
    Console.WriteLine("Waiting for a connection...");
    // Program is suspended while waiting for an incoming connection.
    Socket handler = listener.Accept();
    data = null;

    // An incoming connection needs to be processed.
    while (true) {
        bytes = new byte[1024];
        int bytesRec = handler.Receive(bytes);
        data += Encoding.ASCII.GetString(bytes,0,bytesRec);
        if (data.IndexOf("<EOF>") > -1) {
            break;
            }
        }
    }
like image 788
iAteABug_And_iLiked_it Avatar asked Dec 01 '25 08:12

iAteABug_And_iLiked_it


1 Answers

You are correct. The outer loop in this example is not supposed to exit. This is designed to continually look for new connections. Servers tend to follow this basic pattern.

like image 57
rhughes Avatar answered Dec 02 '25 20:12

rhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!