Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Some notes: Boxes MUST be in the same time zone and MUST be NTP synched
- Client.c
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/timeb.h>
- #define SRV_IP "CHANGE_ME"
- #define PORT 7890
- #define NUM_PACKETS 100
- #define PACKET_DELAY 100
- void diep(char *stringz)
- {
- perror(stringz);
- exit(-1);
- }
- struct s_timestamp {
- int sequence;
- int tseconds;
- int tmilli;
- };
- void delay_time(int PACKETDELAY) {
- int t_diff = 0;
- struct timeb t_delay_s, t_delay_e;
- memset(&t_delay_s, sizeof(struct timeb), 0);
- memset(&t_delay_e, sizeof(struct timeb), 0);
- ftime(&t_delay_s);
- while(t_diff < PACKETDELAY) {
- ftime(&t_delay_e);
- t_diff=(1000 * (t_delay_e.time - t_delay_s.time) + (t_delay_e.millitm - t_delay_s.millitm));
- }
- }
- int main() {
- struct sockaddr_in udp_socket;
- char payload[sizeof(struct timeb)];
- int u_sock, counter, socklen=sizeof(udp_socket);
- struct s_timestamp p_data;
- struct timeb t_current;
- if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
- diep("socket");
- memset((char *)&udp_socket, 0, sizeof(udp_socket));
- udp_socket.sin_family = AF_INET;
- udp_socket.sin_port = htons(PORT);
- if (inet_aton(SRV_IP, &udp_socket.sin_addr)==0) {
- fprintf(stderr, "inet_aton() failed\n");
- exit(1);
- }
- for(counter = 0; counter < NUM_PACKETS; counter++) {
- memset(&p_data, sizeof(struct s_timestamp),0);
- printf("Sending packet %d\n", counter);
- ftime(&t_current);
- p_data.tseconds = t_current.time;
- p_data.tmilli = t_current.millitm;
- p_data.sequence=counter;
- printf("Transmitting packet with sequence number %d\n",p_data.sequence);
- if (sendto(u_sock, (void *)&p_data, sizeof(struct s_timestamp), 0, (struct sockaddr *)&udp_socket, socklen) == -1)
- diep("sendto()");
- delay_time(PACKET_DELAY);
- }
- printf("Size of struct s_timestamp is %d\n",sizeof(struct s_timestamp));
- }
- Server.C:
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/timeb.h>
- #define PORT 7890
- #define NUM_PACKETS 100
- void diep(char *stringz)
- {
- perror(stringz);
- exit(-1);
- }
- struct s_timestamp {
- int sequence;
- int tseconds;
- int tmilli;
- };
- int main() {
- struct sockaddr_in udp_local, udp_socket;
- int u_sock, counter, socklen=sizeof(udp_socket);
- struct s_timestamp p_data;
- struct timeb t_current;
- int delay;
- if((u_sock=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
- diep("socket");
- memset((char *)&udp_socket, 0, sizeof(udp_socket));
- udp_local.sin_family=AF_INET;
- udp_local.sin_port=htons(PORT);
- udp_local.sin_addr.s_addr = htonl(INADDR_ANY);
- if(bind(u_sock, (struct sockaddr *)&udp_local, sizeof(udp_local)) == -1)
- diep("socket");
- while(1) {
- for(counter = 0; counter < NUM_PACKETS; counter++) {
- if(recvfrom(u_sock, (void *)&p_data, sizeof(struct s_timestamp), 0, (struct sockaddr *)&udp_socket, &socklen) == -1)
- diep("recvfrom()");
- printf("Received packet with %d bytes of data with sequence number %d\n",socklen, p_data.sequence);
- printf("Received timestamp %d:%d\n",p_data.tseconds, p_data.tmilli);
- ftime(&t_current);
- delay = (1000 * (t_current.time - p_data.tseconds) + (t_current.millitm - p_data.tmilli));
- printf("Received packet. Packet delay calculated at %d ms\n", delay);
- }
- }
- close(u_sock);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement