This is quite simple thing, but needs good time to explain. You can use TCP. No, the spin wait (the loop you show) is never ever needed. Everything is done according to
push technology: one application is listening for new connection using a blocking call, in a wait state which does not wast any CPU time at all. It's important to listen in one thread, communication in another one.
Client side is simpler: you connect. The call is also blocking, also sleeping wasting no CPU time. When connection is established, you start communicating. You need to define some
application-layer protocol on top of TCP. Please see:
http://en.wikipedia.org/wiki/Application_layer[
^].
See my past answers:
an amateur question in socket programming[
^],
Multple clients from same port Number[
^].
—SA