My Tech notes: UDP Traffic Generator Client Side C code
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Friday, April 3, 2009

UDP Traffic Generator Client Side C code

Learning Networking basics using C programs, to start with here is simple UDP client side code,
Why to start with UDP because its simple, no need of connection handshake like TCP.
This code sends continues UDP traffic over network, depending on the payload (ie send_buf) and delay between each transfer, rate at which data is pumped over network is calculated. Example: this client program is sending constant length 0f 1400 (in sendto function), so if you keep delay as 1s then data transfer rate will be 1400 per sec, decrease the delay to pump traffic at high rate.
You need UDP server side code for running this client.
udp client

#include
#include
#include
#include
#include
#include
#include
#include /* memset() */
#include /* select() */

#define REMOTE_SERVER_PORT 1500
#define MAX_MSG 1600


int main() {

int sd, rc, i;
struct sockaddr_in cliAddr, remoteServAddr;
struct hostent *h;
char *send_buf;
unsigned int count=0;
int reply;

send_buf= (char *) malloc(1600);
*((unsigned int *)send_buf) = count;

strcpy((send_buf+4),"hello how r u !!!!");
remoteServAddr.sin_family = AF_INET;
remoteServAddr.sin_addr.s_addr= inet_addr("1.168.3.100"); //using fixed IP for simplicity
remoteServAddr.sin_port = htons(REMOTE_SERVER_PORT);

/* socket creation */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0)>
printf("cannot open socket \n");
exit(-1);
}

/* send data */
while(1){

*((unsigned int *)send_buf) = ++count;

rc = sendto(sd, send_buf, 1400, 0,(struct sockaddr *) &remoteServAddr,
sizeof(remoteServAddr));
usleep(10000);

if(rc<0)>
printf("cannot send data %d \n");
close(sd);
exit(-1);
}
}
}

No comments:

Post a Comment