Skip to content

Instantly share code, notes, and snippets.

@HarshKumarChoudary
Last active November 20, 2021 12:04
Show Gist options
  • Save HarshKumarChoudary/bcdec916e639ad52c871dad6c0479c91 to your computer and use it in GitHub Desktop.
Save HarshKumarChoudary/bcdec916e639ad52c871dad6c0479c91 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <time.h>
// importing files and time lib is used to measure time interval
int main(){
char *ip = "127.0.0.1";
int port = 5565;
// initializing
int sock;
struct sockaddr_in addr;
socklen_t addr_size;
//initially buffer of size 100 bytes you can change it to get different packet size.
char buffer[100];
int n;
// TCP socket creation and for UDP use SOCK_DGRAM
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("[-]Socket error");
exit(1);
}
printf("[+]TCP server socket created.\n");
memset(&addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = inet_addr(ip);
connect(sock, (struct sockaddr*)&addr, sizeof(addr));
printf("Connected to the server.\n");
int i =1;
printf("Measurements when buffer size is 100 bytes\n");
//two times sending the packets to measure average rtt.
while(i<=2){
bzero(buffer,100);
strcpy(buffer, "HELLO, THIS IS CLIENT.");
clock_t t;
// time starts when sender sends message
t = clock();
send(sock, buffer, strlen(buffer), 0);
bzero(buffer, 100);
recv(sock, buffer, sizeof(buffer), 0);
t = clock() - t;
// time interval calculation in second
double time_taken = ((double)t)/CLOCKS_PER_SEC;
// time ends when message is received back.
printf("this is iteration number%s\n", buffer);
printf("time for this message is %f\n\n", time_taken);
++i;
}
close(sock);
printf("Disconnected from the server.\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment