Tcp.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "api.h"
  2. #include "pal.h"
  3. #include "pal_debug.h"
  4. #define PORT 8000
  5. #define NTRIES 10
  6. int main(int argc, char** argv) {
  7. char addr[40];
  8. int i;
  9. if (argc == 1) {
  10. unsigned long time = DkSystemTimeQuery();
  11. pal_printf("start time = %lu\n", time);
  12. char time_arg[24];
  13. snprintf(time_arg, 24, "%ld", time);
  14. const char* newargs[4] = {"Tcp", time_arg, NULL};
  15. PAL_HANDLE srv = DkStreamOpen("tcp.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:Tcp", newargs);
  23. for (i = 0; i < NTRIES; i++) {
  24. PAL_HANDLE cli = DkStreamWaitForClient(srv);
  25. if (!cli) {
  26. pal_printf("not able to accept client\n");
  27. return -1;
  28. }
  29. DkStreamGetName(cli, addr, 40);
  30. pal_printf("client accepted on %s\n", addr);
  31. int bytes = DkStreamWrite(cli, 0, 12, "Hello World", NULL);
  32. if (!bytes) {
  33. pal_printf("not able to send to client\n");
  34. return -1;
  35. }
  36. DkObjectClose(cli);
  37. }
  38. int retval;
  39. DkStreamRead(proc, 0, sizeof(int), &retval, NULL, 0);
  40. DkStreamDelete(srv, 0);
  41. DkObjectClose(srv);
  42. } else {
  43. for (i = 0; i < NTRIES; i++) {
  44. PAL_HANDLE cli = DkStreamOpen("tcp:127.0.0.1:8000", 0, 0, 0, 0);
  45. if (!cli) {
  46. pal_printf("not able to create client\n");
  47. return -1;
  48. }
  49. DkStreamGetName(cli, addr, 40);
  50. pal_printf("client connected on %s\n", addr);
  51. char buffer[12];
  52. int bytes = DkStreamRead(cli, 0, 12, buffer, NULL, 0);
  53. if (!bytes) {
  54. pal_printf("not able to receive from server\n");
  55. return -1;
  56. }
  57. pal_printf("read from server: %s\n", buffer);
  58. DkStreamDelete(cli, 0);
  59. DkObjectClose(cli);
  60. }
  61. unsigned long end = DkSystemTimeQuery();
  62. pal_printf("end time = %lu\n", end);
  63. unsigned long start = atol(argv[1]);
  64. pal_printf("wall time = %ld\n", end - start);
  65. int retval = 0;
  66. DkStreamWrite(pal_control.parent_process, 0, sizeof(int), &retval, NULL);
  67. }
  68. return 0;
  69. }