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.
#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("");
}
#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 via email to