Udp.c 1.8 KB

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