Socket.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. int main (int argc, char ** argv, char ** envp)
  7. {
  8. char buffer1[20] = "Hello World 1", buffer2[20] = "Hello World 2";
  9. char buffer3[20], buffer4[20];
  10. int ret;
  11. memset(buffer3, 0, 20);
  12. memset(buffer4, 0, 20);
  13. PAL_HANDLE tcp1 = DkStreamOpen("tcp.srv:127.0.0.1:3000", PAL_ACCESS_RDWR, 0, 0, 0);
  14. if (tcp1) {
  15. pal_printf("TCP Creation 1 OK\n");
  16. PAL_HANDLE tcp2 = DkStreamOpen("tcp:127.0.0.1:3000", PAL_ACCESS_RDWR, 0, 0, 0);
  17. if (tcp2) {
  18. PAL_HANDLE tcp3 = DkStreamWaitForClient(tcp1);
  19. if (tcp3) {
  20. pal_printf("TCP Connection 1 OK\n");
  21. ret = DkStreamWrite(tcp3, 0, 20, buffer1, NULL);
  22. if (ret > 0)
  23. pal_printf("TCP Write 1 OK\n");
  24. ret = DkStreamRead(tcp2, 0, 20, buffer3, NULL, 0);
  25. if (ret > 0)
  26. pal_printf("TCP Read 1: %s\n", buffer3);
  27. ret = DkStreamWrite(tcp2, 0, 20, buffer2, NULL);
  28. if (ret > 0)
  29. pal_printf("TCP Write 2 OK\n");
  30. ret = DkStreamRead(tcp3, 0, 20, buffer4, NULL, 0);
  31. if (ret > 0)
  32. pal_printf("TCP Read 2: %s\n", buffer4);
  33. DkObjectClose(tcp3);
  34. }
  35. DkObjectClose(tcp2);
  36. }
  37. DkStreamDelete(tcp1, 0);
  38. DkObjectClose(tcp1);
  39. }
  40. PAL_HANDLE udp1 = DkStreamOpen("udp.srv:127.0.0.1:3000",
  41. PAL_ACCESS_RDWR, 0, 0, 0);
  42. if (udp1) {
  43. pal_printf("UDP Creation 1 OK\n");
  44. PAL_HANDLE udp2 = DkStreamOpen("udp:127.0.0.1:3000",
  45. PAL_ACCESS_RDWR, 0, 0, 0);
  46. if (udp2) {
  47. pal_printf("UDP Connection 1 OK\n");
  48. memset(buffer3, 0, 20);
  49. memset(buffer4, 0, 20);
  50. ret = DkStreamWrite(udp2, 0, 20, buffer1, NULL);
  51. if (ret > 0)
  52. pal_printf("UDP Write 1 OK\n");
  53. char uri[20];
  54. ret = DkStreamRead(udp1, 0, 20, buffer3, uri, 20);
  55. if (ret > 0)
  56. pal_printf("UDP Read 1: %s\n", buffer3);
  57. ret = DkStreamWrite(udp1, 0, 20, buffer2, uri);
  58. if (ret > 0)
  59. pal_printf("UDP Write 2 OK\n");
  60. ret = DkStreamRead(udp2, 0, 20, buffer4, NULL, 0);
  61. if (ret > 0)
  62. pal_printf("UDP Read 2: %s\n", buffer4);
  63. DkObjectClose(udp2);
  64. }
  65. PAL_HANDLE udp3 = DkStreamOpen("udp:127.0.0.1:3001:127.0.0.1:3000",
  66. PAL_ACCESS_RDWR, 0, 0, 0);
  67. if (udp3) {
  68. pal_printf("UDP Connection 2 OK\n");
  69. memset(buffer3, 0, 20);
  70. memset(buffer4, 0, 20);
  71. ret = DkStreamWrite(udp3, 0, 20, buffer1, NULL);
  72. if (ret > 0)
  73. pal_printf("UDP Write 3 OK\n");
  74. char uri[20];
  75. ret = DkStreamRead(udp1, 0, 20, buffer3, uri, 20);
  76. if (ret > 0)
  77. pal_printf("UDP Read 3: %s\n", buffer3);
  78. ret = DkStreamWrite(udp1, 0, 20, buffer2, "udp:127.0.0.1:3001");
  79. if (ret > 0)
  80. pal_printf("UDP Write 4 OK\n");
  81. ret = DkStreamRead(udp3, 0, 20, buffer4, NULL, 0);
  82. if (ret > 0)
  83. pal_printf("UDP Read 4: %s\n", buffer4);
  84. DkObjectClose(udp3);
  85. }
  86. DkStreamDelete(udp1, 0);
  87. DkObjectClose(udp1);
  88. }
  89. return 0;
  90. }