Socket.c 3.5 KB

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