9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [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

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).