Tcp.c 2.5 KB

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