Udp.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. #include "api.h"
  6. #define NTRIES 10
  7. int main (int argc, char ** argv)
  8. {
  9. char addr[40];
  10. int i;
  11. if (argc == 1)
  12. {
  13. unsigned long start = DkSystemTimeQuery();
  14. const char * newargs[3] = { "Udp", "child", NULL };
  15. PAL_HANDLE srv = DkStreamOpen("udp.srv:127.0.0.1:8000", 0, 0, 0, 0);
  16. if (!srv) {
  17. pal_printf("not able to create server\n");
  18. return -1;
  19. }
  20. DkStreamGetName(srv, addr, 40);
  21. pal_printf("server bound on %s\n", addr);
  22. PAL_HANDLE proc = DkProcessCreate("file:Udp", 0, newargs);
  23. for (i = 0 ; i < NTRIES ; i++) {
  24. char buffer[20];
  25. int bytes = DkStreamRead(srv, 0, 20, buffer, addr, 40);
  26. if (!bytes) {
  27. pal_printf("not able to receive from client\n");
  28. return -1;
  29. }
  30. pal_printf("read on server (from %s): %s\n", addr, buffer);
  31. }
  32. unsigned long end = DkSystemTimeQuery();
  33. pal_printf("wall time = %d\n", end - start);
  34. int retval;
  35. DkStreamRead(proc, 0, sizeof(int), &retval, NULL, 0);
  36. DkStreamDelete(srv, 0);
  37. DkObjectClose(srv);
  38. }
  39. else {
  40. PAL_HANDLE cli = DkStreamOpen("udp:127.0.0.1:8000", 0, 0, 0, 0);
  41. DkStreamGetName(cli, addr, 40);
  42. pal_printf("client connected on %s\n", addr);
  43. for (i = 0 ; i < NTRIES ; i++) {
  44. int bytes = DkStreamWrite(cli, 0, 12, "Hello World", NULL);
  45. if (!bytes) {
  46. pal_printf("not able to send to server\n");
  47. return -1;
  48. }
  49. }
  50. DkObjectClose(cli);
  51. int retval = 0;
  52. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &retval, NULL);
  53. }
  54. return 0;
  55. }