-
-
Save Najki/268cdf60ad6c9afcb4cb28c45f30d6e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <arpa/inet.h> | |
#include <unistd.h> | |
#include <netinet/tcp.h> | |
int main(int argc, const char *argv[]) | |
{ | |
if (argc < 4) | |
{ | |
printf("Usage: keepalivetest HOSTNAME PORT KEEPALIVE_TIMEOUT\n"); | |
return 1; | |
} | |
const char *hostname = argv[1]; | |
const int port = atoi(argv[2]); | |
const int idle = atoi(argv[3]); | |
// socket create and varification | |
int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (sockfd == -1) | |
{ | |
printf("Error: Socket creation failed.\n"); | |
return 1; | |
} | |
// enable TCP keep-alive | |
int enablekeepalive = 1; | |
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &enablekeepalive, sizeof(int)) < 0) | |
{ | |
printf("Error: Setting SO_KEEPALIVE failed.\n"); | |
return 1; | |
} | |
//int idle = 15; | |
// int idle = 1; // working setting... | |
if (setsockopt(sockfd, IPPROTO_TCP, /* TCP_KEEPIDLE */ TCP_KEEPALIVE, &idle, sizeof(int)) < 0) | |
{ | |
printf("Error: Setting TCP_KEEPALIVE failed.\n"); | |
return 1; | |
} | |
// int interval = 1; | |
int interval = 75; | |
if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &interval, sizeof(int)) < 0) { | |
printf("Error: Setting TCP_KEEPINTVL failed.\n"); | |
return 1; | |
} | |
// int maxpkt = 10; | |
int maxpkt = 8; | |
if (setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &maxpkt, sizeof(int)) < 0) { | |
printf("Error: Setting TCP_KEEPCNT failed.\n"); | |
return 1; | |
} | |
// assign IP and Port | |
struct sockaddr_in servaddr; | |
bzero(&servaddr, sizeof(servaddr)); | |
servaddr.sin_family = AF_INET; | |
servaddr.sin_addr.s_addr = inet_addr(hostname); | |
servaddr.sin_port = htons(port); | |
// connect the client socket to server socket | |
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) | |
{ | |
printf("Error: Connection with the server failed.\n"); | |
return 1; | |
} | |
else | |
{ | |
printf("Connected to the server.\nPress enter to close the connection... "); | |
} | |
int wait_sec = 80; | |
while (wait_sec > 0) | |
{ | |
sleep(1); | |
printf("."); | |
fflush(stdout); | |
wait_sec -= 1; | |
} | |
printf("\n"); | |
printf("Writing data \n"); | |
const char *buff = "GET / \n\n"; | |
size_t written = 0; | |
printf("about to write %lu \n", strlen(buff)); | |
if ((written = write(sockfd, buff, strlen(buff))) < 0) | |
{ | |
printf("Error write to socket: %d \n", errno); | |
return 1; | |
} | |
printf("written %lu \n", written); | |
char in_buff[1024]; | |
ssize_t chunk_size = 0; | |
ssize_t read_bytes = 0; | |
while ((chunk_size = read(sockfd, &in_buff, sizeof(in_buff))) > 0) | |
{ | |
read_bytes += chunk_size; | |
printf("."); | |
fflush(stdout); | |
} | |
// less `xcrun --show-sdk-path`/usr/include/errno.h | |
printf("Errno: %d \n", errno); | |
printf("Read total bytes: %zd\n", read_bytes); | |
for (;;) | |
{ | |
while (getchar() != '\n') | |
; | |
break; | |
} | |
// close the socket | |
close(sockfd); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment