/* * recvbuf - recv a length-prefixed packet * * This routine receives a message packet in the form * * --------------------------------------- * | length | message | * --------------------------------------- * * where "length" is always exactly 4 bytes. * * * It uses the "MSG_PEEK" flag of _recv to obtain the length field, * placing it in the specified variable. "MSG_PEEK" allows you to * retrieve information from the message without removing it from * the stream. The routine then invokes _recv again, using the * length variable to specify how much data to read from the * socket stream * * * * Modification history: * * 07 May 93 nfnm Initial implementation * 12 Aug 93 pjt Documentation and minor modifications * */ recvbuf: procedure parse arg socket /* * Peek at the message to get its length - put that value in the * variable bufferlength */ recvrc = _recv(socket, "bufferlength", 4, MSG_PEEK) if recvrc < 0 then call error "recv" /* * Actually receive the full message packet, including its length * prefix */ recvrc = _recv(socket, "buffer", bufferlength, "") if recvrc < 0 then call error "recv" /* * Remove the length prefix from the message and return only the * message */ message = substr(buffer,5) return message