I would like to create a simple example TCP server in C. It should be able to receive multiple client connections and to send a string to each of them separately, before closing the connection.
Many code samples like this one use a while(1)
loop to keep listening for incoming client connections. Even in Richard Stevens – Unix Network Programming, Volume 1, Third Edition, Section 5.2, Figure 5.2 it is used for ( ; ; )
, so almost the same.
This is probably the simplest solution, but is it correct from the programmer’s perspective? Aren’t such loops somewhat discouraged?
As an alternative, at least a SIGKILL
could be catched instead and used as a condition for while
, instead of 1
. However, this is somewhat not the first advice in the answers to this question.
The actual question: is there any solution other than while(1)
in this case? Is there a standard one, or does it depend on the specific program? In my case, I would like to keep it as simple as possible, but I would also like to avoid while(1)
.
10