Tcp.c 2.7 KB

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