Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Zaman committed Mar 24, 2014
0 parents commit 309e45e
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 0 deletions.
3 changes: 3 additions & 0 deletions install.sh
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
65 changes: 65 additions & 0 deletions recive.c
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;
}
}
}
75 changes: 75 additions & 0 deletions send.c
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;
}
96 changes: 96 additions & 0 deletions sendforeward.c
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;
}

0 comments on commit 309e45e

Please sign in to comment.