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);
}
--------


Reply via email to