Last active
June 23, 2018 05:14
-
-
Save holmeshe/f8737a7aecbf368fdfe6fa0c5d0f897a to your computer and use it in GitHub Desktop.
This file contains 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 | |
int main(int argc, char *argv[]) { | |
int sfd, portno, n, delay; | |
struct sockaddr_in srvaddr; | |
struct hostent *host; | |
char buffer[256] = "abc"; | |
if (argc < 3) { fprintf(stderr,"usage: %s ip port delay\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_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags))) { perror("ERROR: setsocketopt(), TCP_NODELAY"); exit(0); }; | |
host = gethostbyname(argv[1]); | |
if (host == NULL) { fprintf(stderr,"ERROR: host does not exist"); exit(0); } | |
delay = atoi(argv[3]); | |
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); } | |
for (int i = 0; i < 1000; i++) { | |
n = write(sfd, buffer, 4); | |
if (n < 0) { perror("ERROR: writing()"); exit(0); } | |
usleep(delay); | |
} | |
printf("finished\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment