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;
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With