|
@@ -0,0 +1,116 @@
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <string.h>
|
|
|
|
+#include <sys/socket.h>
|
|
|
|
+#include <netinet/in.h>
|
|
|
|
+#include <arpa/inet.h>
|
|
|
|
+#include <errno.h>
|
|
|
|
+#include <sys/select.h>
|
|
|
|
+
|
|
|
|
+#define SERVER_HOST "127.0.0.1"
|
|
|
|
+#define SERVER_PORT 12345
|
|
|
|
+#define SEND_TIMEOUT_SEC 5
|
|
|
|
+#define RECV_TIMEOUT_SEC 5
|
|
|
|
+
|
|
|
|
+int main()
|
|
|
|
+{
|
|
|
|
+ int sockfd;
|
|
|
|
+ struct sockaddr_in server_addr;
|
|
|
|
+ struct timeval send_timeout, recv_timeout, timeout;
|
|
|
|
+ fd_set read_fds, write_fds;
|
|
|
|
+ int result;
|
|
|
|
+ char send_buffer[] = "Hello, Server!";
|
|
|
|
+ char recv_buffer[1024];
|
|
|
|
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
+ if (sockfd < 0)
|
|
|
|
+ {
|
|
|
|
+ perror("Socket creation failed");
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ memset(&server_addr, 0, sizeof(server_addr));
|
|
|
|
+ server_addr.sin_family = AF_INET;
|
|
|
|
+ server_addr.sin_port = htons(SERVER_PORT);
|
|
|
|
+ if (inet_pton(AF_INET, SERVER_HOST, &server_addr.sin_addr) <= 0)
|
|
|
|
+ {
|
|
|
|
+ perror("Invalid address");
|
|
|
|
+ close(sockfd);
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
|
|
|
|
+ {
|
|
|
|
+ perror("Connection failed");
|
|
|
|
+ close(sockfd);
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ // Set timeouts for sending and receiving
|
|
|
|
+ send_timeout.tv_sec = SEND_TIMEOUT_SEC;
|
|
|
|
+ send_timeout.tv_usec = 0;
|
|
|
|
+ recv_timeout.tv_sec = RECV_TIMEOUT_SEC;
|
|
|
|
+ recv_timeout.tv_usec = 0;
|
|
|
|
+ // Set socket option to configure timeout for send and receive
|
|
|
|
+ setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &recv_timeout, sizeof(recv_timeout));
|
|
|
|
+ setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &send_timeout, sizeof(send_timeout));
|
|
|
|
+ // Send data with timeout
|
|
|
|
+ FD_ZERO(&write_fds);
|
|
|
|
+ FD_SET(sockfd, &write_fds);
|
|
|
|
+ timeout = send_timeout;
|
|
|
|
+ //result = select(sockfd + 1, NULL, &write_fds, NULL, &timeout);
|
|
|
|
+ // if (result < 0)
|
|
|
|
+ // {
|
|
|
|
+ // perror("Select error");
|
|
|
|
+ // close(sockfd);
|
|
|
|
+ //return 1;
|
|
|
|
+ //}
|
|
|
|
+ //else if (result == 0)
|
|
|
|
+ // {
|
|
|
|
+ // printf("Send operation timed out\n");
|
|
|
|
+ // close(sockfd);
|
|
|
|
+ // return 1;
|
|
|
|
+ //}
|
|
|
|
+ //else if (FD_ISSET(sockfd, &write_fds))
|
|
|
|
+ // {
|
|
|
|
+ if (send(sockfd, send_buffer, strlen(send_buffer), 0) == -1)
|
|
|
|
+ {
|
|
|
|
+ perror("Send failed");
|
|
|
|
+ close(sockfd);
|
|
|
|
+ return 1;
|
|
|
|
+ }
|
|
|
|
+ printf("Message sent to server: %s\n", send_buffer);
|
|
|
|
+ // }
|
|
|
|
+ // Receive data with timeout
|
|
|
|
+ FD_ZERO(&read_fds);
|
|
|
|
+ FD_SET(sockfd, &read_fds);
|
|
|
|
+ timeout = recv_timeout;
|
|
|
|
+ // result = select(sockfd + 1, &read_fds, NULL, NULL, &timeout);
|
|
|
|
+ // if (result < 0)
|
|
|
|
+ // {
|
|
|
|
+ // perror("Select error");
|
|
|
|
+ // close(sockfd);
|
|
|
|
+ // return 1;
|
|
|
|
+ // } else if (result == 0)
|
|
|
|
+ //{
|
|
|
|
+ // printf("Receive operation timed out\n");
|
|
|
|
+ // close(sockfd);
|
|
|
|
+ // return 1;
|
|
|
|
+ // }
|
|
|
|
+ // else if (FD_ISSET(sockfd, &read_fds))
|
|
|
|
+ // {
|
|
|
|
+ int bytes_received = recv(sockfd, recv_buffer, sizeof(recv_buffer) - 1, 0);
|
|
|
|
+ if (bytes_received < 0)
|
|
|
|
+ {
|
|
|
|
+ perror("Receive failed");
|
|
|
|
+ }
|
|
|
|
+ else if (bytes_received == 0)
|
|
|
|
+ {
|
|
|
|
+ printf("Server closed the connection\n");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ recv_buffer[bytes_received] = '\0';
|
|
|
|
+ printf("Received from server: %s\n", recv_buffer);
|
|
|
|
+ }
|
|
|
|
+ // }
|
|
|
|
+close(sockfd);
|
|
|
|
+return 0;
|
|
|
|
+}
|