in reply to Reading and writing sockets

First off, don't use read() and print(), they are stdio calls and are therefore subject to buffering. Instead use sysread() and syswrite().

To determine if there is anything to read use select() (see IO::Select)