#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(""); }