#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>		/* ntohl is found here for RT's */
/*
 * Routines to send and receive on sockets.  Four bytes of length are
 * sent, followed by the null terminated string.
 *
 */
read_socket(s, buf)
int	s;			/* socket to talk on */
char	*buf;			/* string to send */
{
	int nbytes = 0;

	if (nread(s, (char *)&nbytes, sizeof(int)) == sizeof(int))
		return 0;
	nbytes = ntohl(nbytes);
	if (nread(s, buf, nbytes) != nbytes)
		return 0;
	return nbytes;
}


write_socket(s, buf)
int	s;			/* socket to talk on */
char	*buf;			/* string to read on */
{
	int nbytes, netnbytes;

	nbytes = strlen(buf) + 1;
	netnbytes = htonl(nbytes);
	if (write(s, (char *)&netnbytes, sizeof(int)) != sizeof(int) ||
	    write(s, buf, nbytes) != nbytes)
		HOOT;
}

/* fread for file descriptors: returns size bytes except at EOF. */
int
nread(fd, buf, size)
register int fd, size;
char *buf;
{
	register int bytes, sofar = 0;

	while (size - sofar > 0) {
		bytes = read(fd, buf + sofar, size - sofar);
		if (bytes < 0)
			return -1;
		if (bytes == 0)		/* EOF: return what we have */
			break;
		sofar += bytes;
	}
	return sofar;
}
