client_non_blocking_SR.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/socket.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <errno.h>
  9. #include <sys/select.h>
  10. #define SERVER_HOST "127.0.0.1"
  11. #define SERVER_PORT 12345
  12. #define SEND_TIMEOUT_SEC 5
  13. #define RECV_TIMEOUT_SEC 5
  14. int main()
  15. {
  16. int sockfd;
  17. struct sockaddr_in server_addr;
  18. struct timeval send_timeout, recv_timeout, timeout;
  19. fd_set read_fds, write_fds;
  20. int result;
  21. char send_buffer[] = "Hello, Server!";
  22. char recv_buffer[1024];
  23. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  24. if (sockfd < 0)
  25. {
  26. perror("Socket creation failed");
  27. return 1;
  28. }
  29. memset(&server_addr, 0, sizeof(server_addr));
  30. server_addr.sin_family = AF_INET;
  31. server_addr.sin_port = htons(SERVER_PORT);
  32. if (inet_pton(AF_INET, SERVER_HOST, &server_addr.sin_addr) <= 0)
  33. {
  34. perror("Invalid address");
  35. close(sockfd);
  36. return 1;
  37. }
  38. if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
  39. {
  40. perror("Connection failed");
  41. close(sockfd);
  42. return 1;
  43. }
  44. // Set timeouts for sending and receiving
  45. send_timeout.tv_sec = SEND_TIMEOUT_SEC;
  46. send_timeout.tv_usec = 0;
  47. recv_timeout.tv_sec = RECV_TIMEOUT_SEC;
  48. recv_timeout.tv_usec = 0;
  49. // Set socket option to configure timeout for send and receive
  50. setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recv_timeout, sizeof(recv_timeout));
  51. setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_timeout, sizeof(send_timeout));
  52. // Send data with timeout
  53. FD_ZERO(&write_fds);
  54. FD_SET(sockfd, &write_fds);
  55. timeout = send_timeout;
  56. //result = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
  57. // if (result < 0)
  58. // {
  59. // perror("Select error");
  60. // close(sockfd);
  61. //return 1;
  62. //}
  63. //else if (result == 0)
  64. // {
  65. // printf("Send operation timed out\n");
  66. // close(sockfd);
  67. // return 1;
  68. //}
  69. //else if (FD_ISSET(sockfd, &write_fds))
  70. // {
  71. if (send(sockfd, send_buffer, strlen(send_buffer), 0) == -1)
  72. {
  73. perror("Send failed");
  74. close(sockfd);
  75. return 1;
  76. }
  77. printf("Message sent to server: %s\n", send_buffer);
  78. // }
  79. // Receive data with timeout
  80. FD_ZERO(&read_fds);
  81. FD_SET(sockfd, &read_fds);
  82. timeout = recv_timeout;
  83. // result = select(sockfd + 1, &read_fds, NULL, NULL, &timeout);
  84. // if (result < 0)
  85. // {
  86. // perror("Select error");
  87. // close(sockfd);
  88. // return 1;
  89. // } else if (result == 0)
  90. //{
  91. // printf("Receive operation timed out\n");
  92. // close(sockfd);
  93. // return 1;
  94. // }
  95. // else if (FD_ISSET(sockfd, &read_fds))
  96. // {
  97. int bytes_received = recv(sockfd, recv_buffer, sizeof(recv_buffer) - 1, 0);
  98. if (bytes_received < 0)
  99. {
  100. perror("Receive failed");
  101. }
  102. else if (bytes_received == 0)
  103. {
  104. printf("Server closed the connection\n");
  105. }
  106. else
  107. {
  108. recv_buffer[bytes_received] = '\0';
  109. printf("Received from server: %s\n", recv_buffer);
  110. }
  111. // }
  112. close(sockfd);
  113. return 0;
  114. }