Need reliable datagrams

Chris Torek chris at umcp-cs.UUCP
Tue Oct 7 12:30:26 AEST 1986


In article <3189 at teddy.UUCP> jpn at teddy.UUCP (John P. Nelson) writes:
>I have an application that needs to reliably send packets of data
>around among several tasks in a system.
...
>My SUN manual lists a SOCK_SEQPACKET protocol as being "planned but
>not implemented"; this looks like just what I need.  Too bad.

>We have all kinds of source licences here:  BSD4.2, BSD4.3, ULTRIX 1.2,
>and SUN 3.0.  Any kind of pointers would be appreciated.

While this works only under 4.3 systems, you can use AF_XNS sockets
to send packets.  SPP sockets are indeed SEQPACKET sockets.

	int s = socket(AF_XNS, SOCK_SEQPACKET, 0);

	if (s < 0)
		gripe();

You had best read the new IPC primer first.

Alternatively, you can drop your own `packet' protocol on top
of TCP:

/*
 * Write a message packet.
 */
void
wrmsg(fd, buf, size)
	int fd;
	char *buf;
	int size;
{
	long nsize = htonl(size);

	/*
	 * Could use writev here, for efficiency...
	 */
	if (write(fd, (char *) &nsize, sizeof (nsize)) != sizeof (nsize) ||
	    write(fd, buf, size) != size)
		gripe();
}

/*
 * `reliable' read
 */
void
rread(fd, buf, n)
	int fd;
	char *buf;
	int n;
{
	int r;

	while (n > 0) {
		r = read(fd, buf, n);
		if (r <= 0)
			gripe();
		buf += r;
		n -= r;
	}
}

int
rdmsg(fd, buf, size)
	int fd;
	char *buf;
	int size;
{
	long nsize, discard;
	char trash[BUFSIZ];

	rread(fd, (char *) &nsize, sizeof (nsize));
	nsize = ntohl(nsize);
	discard = nsize - size;
	if (nsize > size)
		nsize = size;
	size = nsize;
	rread(fd, buf, (int) nsize);
	while (discard > 0) {
		nsize = discard > BUFSIZ ? BUFSIZ : discard;
		nsize = read(fd, trash, (int) nsize);
		if (nsize <= 0)
			gripe();
		discard -= nsize;
	}
	return (size);
}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list