-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Muhammad Zaman
committed
Mar 24, 2014
0 parents
commit 309e45e
Showing
4 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
g++ recive.c -o recive -lrt | ||
g++ send.c -o send -lrt | ||
g++ sendforeward.c -o foreward -lrt |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <iostream> | ||
#include <string> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <netdb.h> | ||
#include <time.h> | ||
#include <stdint.h> | ||
using namespace std; | ||
|
||
int64_t nano_count() | ||
{ | ||
struct timespec t; | ||
int ret; | ||
ret = clock_gettime(CLOCK_MONOTONIC,&t); | ||
if(ret != 0) | ||
cout<<"clock_gettime failed"<<endl; | ||
return t.tv_sec * 1000000000 + t.tv_nsec; | ||
} | ||
|
||
int main(){ | ||
|
||
int my_port = 54312; | ||
|
||
int recvlen; | ||
char* buf[10]; | ||
int s; | ||
|
||
int buf_size = 30; | ||
|
||
struct sockaddr_in remaddr; | ||
struct sockaddr_in myaddr; | ||
socklen_t addrlen = sizeof(remaddr); | ||
if( (s = socket(AF_INET, SOCK_DGRAM, 0)) <0){ | ||
cout<<"socket failed"<<endl; | ||
return 0; | ||
} | ||
|
||
|
||
memset((char *)&myaddr, 0, sizeof(myaddr)); | ||
myaddr.sin_family = AF_INET; | ||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
myaddr.sin_port = htons(my_port); | ||
|
||
if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { | ||
cout<<"bind failed"<<endl; | ||
return 0; | ||
} | ||
|
||
int64_t time_start = nano_count(); | ||
for(int i=0;;i++){ | ||
if(i==0) | ||
time_start = nano_count(); | ||
recvfrom(s, buf, buf_size, 0, (struct sockaddr *)&remaddr, &addrlen); | ||
if(i%1000 == 0){ | ||
int64_t time_end = nano_count(); | ||
cout<<"number of packets recived and forewarded "<< i <<endl; | ||
cout<<"this took "<<time_end-time_start<<" nanoseconds"<<endl; | ||
cout<<(time_end-time_start)/1000<<" microseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000<<" milliseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000000<<" seconds"<<endl; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <iostream> | ||
#include <string> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <netdb.h> | ||
#include <time.h> | ||
#include <stdint.h> | ||
using namespace std; | ||
|
||
int64_t nano_count() | ||
{ | ||
struct timespec t; | ||
int ret; | ||
ret = clock_gettime(CLOCK_MONOTONIC,&t); | ||
if(ret != 0) | ||
cout<<"clock_gettime failed"<<endl; | ||
return t.tv_sec * 1000000000 + t.tv_nsec; | ||
} | ||
|
||
int main(){ | ||
|
||
char* host_addr = "pc481.emulab.net"; | ||
int host_port = 54312; | ||
int num_packets = 1000; | ||
|
||
int s; | ||
|
||
if( (s = socket(AF_INET, SOCK_DGRAM, 0)) <0){ | ||
cout<<"socket failed"<<endl; | ||
return 0; | ||
} | ||
|
||
struct sockaddr_in myaddr; | ||
|
||
memset((char *)&myaddr, 0, sizeof(myaddr)); | ||
myaddr.sin_family = AF_INET; | ||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
myaddr.sin_port = htons(0); | ||
|
||
if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { | ||
cout<<"bind failed"<<endl; | ||
return 0; | ||
} | ||
|
||
struct hostent *hp; | ||
struct sockaddr_in servaddr; | ||
|
||
char *my_message = "1"; | ||
|
||
memset((char*)&servaddr, 0, sizeof(servaddr)); | ||
servaddr.sin_family = AF_INET; | ||
servaddr.sin_port = htons(host_port); | ||
|
||
hp = gethostbyname(host_addr); | ||
if (!hp) { | ||
cout<<"gethostbyname failed"<<endl; | ||
return 0; | ||
} | ||
memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); | ||
|
||
int64_t time_start = nano_count(); | ||
for(int i=0;i<num_packets*1000;i++){ | ||
if (sendto(s, my_message, strlen(my_message), 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { | ||
cout<<"sendto failed"<<endl; | ||
return 0; | ||
} | ||
} | ||
int64_t time_end = nano_count(); | ||
cout<<"this took "<<time_end-time_start<<" nanoseconds"<<endl; | ||
cout<<(time_end-time_start)/1000<<" microseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000<<" milliseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000000<<" seconds"<<endl; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include <sys/types.h> | ||
#include <sys/socket.h> | ||
#include <iostream> | ||
#include <string> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <netdb.h> | ||
#include <time.h> | ||
#include <stdint.h> | ||
using namespace std; | ||
|
||
int64_t nano_count() | ||
{ | ||
struct timespec t; | ||
int ret; | ||
ret = clock_gettime(CLOCK_MONOTONIC,&t); | ||
if(ret != 0) | ||
cout<<"clock_gettime failed"<<endl; | ||
return t.tv_sec * 1000000000 + t.tv_nsec; | ||
} | ||
|
||
int main(){ | ||
|
||
char* host_addr = "pc477.emulab.net"; | ||
int host_port = 54312; | ||
int num_packets = 1000; | ||
|
||
int my_port = 54312; | ||
|
||
int recvlen; | ||
char* buf[10]; | ||
int s; | ||
|
||
int buf_size = 30; | ||
|
||
struct sockaddr_in remaddr; | ||
struct sockaddr_in myaddr; | ||
struct sockaddr_in servaddr; | ||
socklen_t addrlen = sizeof(remaddr); | ||
if( (s = socket(AF_INET, SOCK_DGRAM, 0)) <0){ | ||
cout<<"socket failed"<<endl; | ||
return 0; | ||
} | ||
|
||
|
||
memset((char *)&myaddr, 0, sizeof(myaddr)); | ||
myaddr.sin_family = AF_INET; | ||
myaddr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
myaddr.sin_port = htons(my_port); | ||
|
||
if (bind(s, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) { | ||
cout<<"bind failed"<<endl; | ||
return 0; | ||
} | ||
|
||
struct hostent *hp; | ||
|
||
char *my_message = "1"; | ||
|
||
memset((char*)&servaddr, 0, sizeof(servaddr)); | ||
servaddr.sin_family = AF_INET; | ||
servaddr.sin_port = htons(host_port); | ||
|
||
hp = gethostbyname(host_addr); | ||
if (!hp) { | ||
cout<<"gethostbyname failed"<<endl; | ||
return 0; | ||
} | ||
memcpy((void *)&servaddr.sin_addr, hp->h_addr_list[0], hp->h_length); | ||
|
||
int64_t time_start = nano_count(); | ||
int64_t time_end; | ||
for(int i=0;i<num_packets*950;i++){ | ||
if(i==0) | ||
time_start = nano_count(); | ||
if ( (recvlen = recvfrom(s, buf, buf_size, 0, (struct sockaddr *)&remaddr, &addrlen) > 0)){ | ||
if (sendto(s, buf, recvlen, 0, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0){ | ||
cout<<"sendto failed"<<endl; | ||
return 0; | ||
} | ||
} | ||
if(i%1000 == 0){ | ||
time_end = nano_count(); | ||
cout<<"number of packets recived and forewarded "<< i <<endl; | ||
cout<<"this took "<<time_end-time_start<<" nanoseconds"<<endl; | ||
cout<<(time_end-time_start)/1000<<" microseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000<<" milliseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000000<<" seconds"<<endl; | ||
} | ||
} | ||
time_end = nano_count(); | ||
cout<<"this took "<<time_end-time_start<<" nanoseconds"<<endl; | ||
cout<<(time_end-time_start)/1000<<" microseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000<<" milliseconds"<<endl; | ||
cout<<(time_end-time_start)/1000000000<<" seconds"<<endl; | ||
} |