* [9fans] UDP insight needed..
@ 2008-12-01 19:24 Ishwar Rattan
2008-12-01 22:20 ` erik quanstrom
0 siblings, 1 reply; 3+ messages in thread
From: Ishwar Rattan @ 2008-12-01 19:24 UTC (permalink / raw)
To: 9fans
Trying to get UDP working with announce.dial and friends.
Code for an attempted UDP server/client listed below.
The server does not get beyond the read() call -- as if the
client never got connected. Dial() in client does not return
error. Any pointers will bve appreciated.
-ishwar
-------server.c---
/* Example of a server, echoes back the
* info received from a client
* It does not complete the read()..
*/
#include <u.h>
#include <libc.h>
#include <ip.h>
void
main(void)
{
int afd, lcfd;
char adir[40], ldir[40];
int i, n;
char buf[256];
afd = announce("udp!*!8001", adir);
if(afd < 0) sysfatal("announce..\n");
if(fprint(afd, "headers") < 0)
fprint(2, "headers..\n");
sprint(ldir, "%s/data", adir);
lcfd = open(ldir, ORDWR);
if(lcfd < 0) {
close(afd); sysfatal("listen..\n");
}
n = read(lcfd, buf, 256);
if(n <=0) {
close(afd); close(lcfd); sysfatal("read..\n");
}
for(i = 0; i < 12; i++)
print("%x ", (uchar *)(buf+i));
print("\n");
close(afd); close(lcfd);
exits(nil);
}
--------client.c---
/* Client code for echo client..
*/
#include <u.h>
#include <libc.h>
void
main(void)
{
char buf[128], *string = "Message to server.";
int fd, len;
if((fd = dial("udp!localhost!8001", 0, 0, 0)) < 0)
sysfatal("dial");
print("after dial..\n");
srand((long)i);
sprint(buf, "%s%d", string, rand());
len = strlen(buf) + 1;
if(write(fd, buf, len) != len) {
close(fd); sysfatal("write..\n"); }
buf[0] = 0;
read(fd, buf, 128);
close(fd);
exits(nil);
}
--------
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [9fans] UDP insight needed..
2008-12-01 19:24 [9fans] UDP insight needed Ishwar Rattan
@ 2008-12-01 22:20 ` erik quanstrom
2008-12-01 22:53 ` Gabriel Diaz Lopez de la Llave
0 siblings, 1 reply; 3+ messages in thread
From: erik quanstrom @ 2008-12-01 22:20 UTC (permalink / raw)
To: 9fans
[-- 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);
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [9fans] UDP insight needed..
2008-12-01 22:20 ` erik quanstrom
@ 2008-12-01 22:53 ` Gabriel Diaz Lopez de la Llave
0 siblings, 0 replies; 3+ messages in thread
From: Gabriel Diaz Lopez de la Llave @ 2008-12-01 22:53 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
Hello
I think loopback is needed by venti, isn't it?
gabi
El 01/12/2008, a las 23:20, erik quanstrom escribió:
> 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.
> <udpclient.c><udpserver.c>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-12-01 22:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-12-01 19:24 [9fans] UDP insight needed Ishwar Rattan
2008-12-01 22:20 ` erik quanstrom
2008-12-01 22:53 ` Gabriel Diaz Lopez de la Llave
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).