Udp.c 1.8 KB

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