9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@quanstro.net>
To: 9fans@9fans.net
Subject: Re: [9fans] UDP insight needed..
Date: Mon,  1 Dec 2008 17:20:11 -0500	[thread overview]
Message-ID: <3115d689b59f1309a13f789d6df536ab@quanstro.net> (raw)
In-Reply-To: <Pine.LNX.4.64.0812011420190.6464@pali.cps.cmich.edu>

[-- Attachment #1: Type: text/plain, Size: 349 bytes --]

i believe the calls to listen an accept were
missing.  i added a function to the client to
determine the local address so we don't have to
depend on localhost.  (which is never set on my
machines.) and i added a function to the server to
print the caller.

- erik

ps.  would some humane soul please take localhost
out back and shoot it.

[-- Attachment #2: udpclient.c --]
[-- Type: text/plain, Size: 842 bytes --]

#include <u.h>
#include <libc.h>
#include <ip.h>

int
findip(char *s, int len)
{
	Ipifc *ifc;
	Iplifc *l;

	fmtinstall('I', eipfmt);
	for(ifc = readipifc("/net", 0, 0); ifc; ifc = ifc->next)
		for(l = ifc->lifc; l; l = l->next)
			if(l->ip){
//				freeipifc(ifc);
				snprint(s, len, "udp!%I!8001", l->ip);
				return 0;
			}
//	freeipifc(ifc);
	return -1;
}

void
main(void)
{
	char buf[128], ds[64];
	int n, fd, len;

	if(findip(ds, sizeof ds) == -1)
		sysfatal("can't find my ip");
	if((fd = dial(ds, 0, 0, 0)) == -1)
		sysfatal("dial: %r");
	srand(time(0));
	len = sprint(buf, "Message to server%d", rand());
	if(write(fd, buf, len) != len)
		sysfatal("write: %r");
	if((n = read(fd, buf, sizeof buf)) == -1)
		sysfatal("read: %r");
	fprint(2, "read [%.*s]\n", n, buf);

	close(fd);
	exits("");
}

[-- Attachment #3: udpserver.c --]
[-- Type: text/plain, Size: 997 bytes --]

#include <u.h>
#include <libc.h>
#include <ip.h>

char*
remoteaddr(char *dir)
{
	static char buf[128];
	char *p;
	int n, fd;

	snprint(buf, sizeof buf, "%s/remote", dir);
	fd = open(buf, OREAD);
	if(fd < 0)
		return "";
	n = read(fd, buf, sizeof(buf));
	close(fd);
	if(n > 0){
		buf[n] = 0;
		p = strchr(buf, '!');
		if(p)
			*p = 0;
		return buf;
	}
	return "";
}

void
main(void)
{
	char buf[512], dir[40], ndir[40];
	int n, ctl, nctl, fd;

	if((ctl = announce("udp!*!8001", dir)) == -1)
		sysfatal("announce: %r");
	nctl = listen(dir, ndir);
	if(nctl < 0)
		sysfatal("listen %r");

	fd = accept(nctl, ndir);
	if(fd < 0)
		sysfatal("accept: can't open  %s/data: %r", ndir);
	fprint(2, "incoming call from %s in %s\n", remoteaddr(ndir), ndir);

	if((n = read(fd, buf, sizeof buf)) == -1)
		sysfatal("read: %r");
	fprint(2, "read [%.*s]\n", n, buf);

	if(write(fd, buf, n) != n)
		sysfatal("read: %r");

	close(ctl);
	close(nctl);
	exits(0);
}

  reply	other threads:[~2008-12-01 22:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-01 19:24 Ishwar Rattan
2008-12-01 22:20 ` erik quanstrom [this message]
2008-12-01 22:53   ` Gabriel Diaz Lopez de la Llave

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3115d689b59f1309a13f789d6df536ab@quanstro.net \
    --to=quanstro@quanstro.net \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).