From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <3115d689b59f1309a13f789d6df536ab@quanstro.net> To: 9fans@9fans.net From: erik quanstrom Date: Mon, 1 Dec 2008 17:20:11 -0500 In-Reply-To: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="upas-pjcfekwhokxfntupxltfptzejq" Subject: Re: [9fans] UDP insight needed.. Topicbox-Message-UUID: 5556bf26-ead4-11e9-9d60-3106f5b1d025 This is a multi-part message in MIME format. --upas-pjcfekwhokxfntupxltfptzejq Content-Disposition: inline Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit 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. --upas-pjcfekwhokxfntupxltfptzejq Content-Disposition: attachment; filename=udpclient.c Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit #include #include #include 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(""); } --upas-pjcfekwhokxfntupxltfptzejq Content-Disposition: attachment; filename=udpserver.c Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit #include #include #include 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); } --upas-pjcfekwhokxfntupxltfptzejq--