Socket.c 3.4 KB

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