Skip to content

Instantly share code, notes, and snippets.

@holmeshe
Created June 4, 2018 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holmeshe/a8186dd3020840dc4337f7b139da4550 to your computer and use it in GitHub Desktop.
Save holmeshe/a8186dd3020840dc4337f7b139da4550 to your computer and use it in GitHub Desktop.
...include
int main(int argc, char *argv[]) {
int sfd, portno, n;
struct sockaddr_in srvaddr;
struct hostent *host;
if (argc < 3) { fprintf(stderr,"usage: %s ip port\n", argv[0]); exit(0); }
portno = atoi(argv[2]);
sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0) { perror("ERROR: socket()"); exit(0); }
int flags =1;
if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPALIVE"); exit(0); };
flags = 10;
if (setsockopt(sfd, SOL_TCP, TCP_KEEPIDLE, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), SO_KEEPIDLE"); exit(0); };
host = gethostbyname(argv[1]);
if (host == NULL) { fprintf(stderr,"ERROR: host does not exist"); exit(0); }
bzero((char *) &srvaddr, sizeof(srvaddr));
srvaddr.sin_family = AF_INET;
srvaddr.sin_port = htons(portno);
bcopy((char *)host->h_addr, (char *)&srvaddr.sin_addr.s_addr, host->h_length);
if (connect(sfd, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) < 0) { perror("ERROR: connect()"); exit(0); }
sleep(100000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment