//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Boost Shared Memory Reader
// This vector is read from a shared memory
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iterator>
#include <functional>
#include <utility>
#include <sys/time.h>
#include <time.h>
#include <iostream>
#define NUM_ENTRIES 10
int main ()
{
using namespace boost::interprocess;
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
typedef vector<int, ShmemAllocator> MyVector;
try{
//Open the managed segment
managed_shared_memory segment(open_only, "MySharedMemory");
//Find the vector using the c-string name
MyVector *myvector = segment.find<MyVector>("MyVector").first;
//size of vector
std::cout << "size = " << myvector->size() << std::endl;
MyVector::iterator iter = myvector->begin();
while(iter++ != myvector->end())
std::cout << "nvalue = " << *iter ;
//When done, destroy the vector from the segment
//segment.destroy<MyVector>("MyVector");
}
catch(...){
std::cout<<"n Exception caught";
throw;
}
return 0;
}
Monday, December 21, 2009
Boost Shared Memory Vector
/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Boost Shared Memory
// This vector is stored in a shared memory
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
using namespace boost::interprocess;
/* Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
This allocator will allow placing containers in the segment */
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
/* Alias a vector that uses the previous STL-like allocator so that allocates
its values from the segment */
typedef vector<int, ShmemAllocator> MyVector;
/* Main function. For parent process argc == 1, for child process argc == 2 */
int main(int argc, char *argv[])
{
shared_memory_object::remove("MySharedMemory");
//Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//Initialize shared memory STL-compatible allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager());
//Construct a vector named "MyVector" in shared memory with argument alloc_inst
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
for(int i = 0; i < 100; ++i) //Insert data in the vector
myvector->push_back(i);
return 0;
}
// Boost Shared Memory
// This vector is stored in a shared memory
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/containers/vector.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <string>
#include <cstdlib> //std::system
using namespace boost::interprocess;
/* Define an STL compatible allocator of ints that allocates from the managed_shared_memory.
This allocator will allow placing containers in the segment */
typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator;
/* Alias a vector that uses the previous STL-like allocator so that allocates
its values from the segment */
typedef vector<int, ShmemAllocator> MyVector;
/* Main function. For parent process argc == 1, for child process argc == 2 */
int main(int argc, char *argv[])
{
shared_memory_object::remove("MySharedMemory");
//Create a new segment with given name and size
managed_shared_memory segment(create_only, "MySharedMemory", 65536);
//Initialize shared memory STL-compatible allocator
const ShmemAllocator alloc_inst (segment.get_segment_manager());
//Construct a vector named "MyVector" in shared memory with argument alloc_inst
MyVector *myvector = segment.construct<MyVector>("MyVector")(alloc_inst);
for(int i = 0; i < 100; ++i) //Insert data in the vector
myvector->push_back(i);
return 0;
}
Producer Consumer Problem
/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// A Producer Consumer Problem
// Message Queue with single producer and single consumer
// Try the same program without using locks
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <pthread.h>
#include <queue>
#include <stdio.h>
#include <unistd.h>
#define MAX_SIZE 100000
using namespace std;
pthread_cond_t condition;
pthread_mutex_t my_mutex;
queue <int> cb;
void *ProducerFunc(void *x)
{
// Insert some elements into the buffer.
printf("Producer Threadn");
int i;
for (i=0;i<MAX_SIZE;i++)
{
//printf("inside for loop Producer Threadn");
pthread_mutex_lock(&my_mutex);
cb.push(i);
pthread_cond_signal(&condition);
pthread_mutex_unlock(&my_mutex);
}
pthread_exit(NULL);
}
void *ConsumerFunc(void *x)
{
printf("Consumer Threadn");
int i;
while(1)
{
pthread_mutex_lock(&my_mutex);
pthread_cond_wait(&condition, &my_mutex);
while(!cb.empty())
{
printf("Deleted value is = %dn",cb.front());
cb.pop();
}
pthread_mutex_unlock(&my_mutex);
}
pthread_exit(NULL);
}
int main()
{
pthread_t producer, consumer;
pthread_cond_init(&condition, NULL);
pthread_mutex_init(&my_mutex, NULL);
int rc1 = pthread_create(&producer, NULL, ProducerFunc, NULL);
int rc2 = pthread_create(&consumer, NULL, ConsumerFunc, NULL);
if (rc1 || rc2)
{
printf("ERROR; return code from pthread_create() is %dn", rc1);
return 0;
}
pthread_join(producer, NULL);
pthread_join(consumer, NULL);
pthread_mutex_destroy(&my_mutex);
pthread_cond_destroy(&condition);
pthread_exit(NULL);
return 0;
}
// A Producer Consumer Problem
// Message Queue with single producer and single consumer
// Try the same program without using locks
// Date : 26 - July -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <pthread.h>
#include <queue>
#include <stdio.h>
#include <unistd.h>
#define MAX_SIZE 100000
using namespace std;
pthread_cond_t condition;
pthread_mutex_t my_mutex;
queue <int> cb;
void *ProducerFunc(void *x)
{
// Insert some elements into the buffer.
printf("Producer Threadn");
int i;
for (i=0;i<MAX_SIZE;i++)
{
//printf("inside for loop Producer Threadn");
pthread_mutex_lock(&my_mutex);
cb.push(i);
pthread_cond_signal(&condition);
pthread_mutex_unlock(&my_mutex);
}
pthread_exit(NULL);
}
void *ConsumerFunc(void *x)
{
printf("Consumer Threadn");
int i;
while(1)
{
pthread_mutex_lock(&my_mutex);
pthread_cond_wait(&condition, &my_mutex);
while(!cb.empty())
{
printf("Deleted value is = %dn",cb.front());
cb.pop();
}
pthread_mutex_unlock(&my_mutex);
}
pthread_exit(NULL);
}
int main()
{
pthread_t producer, consumer;
pthread_cond_init(&condition, NULL);
pthread_mutex_init(&my_mutex, NULL);
int rc1 = pthread_create(&producer, NULL, ProducerFunc, NULL);
int rc2 = pthread_create(&consumer, NULL, ConsumerFunc, NULL);
if (rc1 || rc2)
{
printf("ERROR; return code from pthread_create() is %dn", rc1);
return 0;
}
pthread_join(producer, NULL);
pthread_join(consumer, NULL);
pthread_mutex_destroy(&my_mutex);
pthread_cond_destroy(&condition);
pthread_exit(NULL);
return 0;
}
Boost Message Queue (Interprocess) Receiver
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// InterProcess Message Queue with Boost::interprocess
// Message Queue Receiver
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ mq_receiver.cpp -o mq_receiver -I path_to_boost -lboost_system-gcc43-mt-1_39
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
using namespace boost::interprocess;
// Structure to be read
typedef struct s_Request{
int src_ip,des_ip;
boost::asio::ip::address objRequest;
char name[50];
}st_Request;
int main ()
{
try{
//Open a message queue.
message_queue mq
(open_only //only create
,"message_queue" //name
);
unsigned int priority;
std::size_t recvd_size;
//Receive 100 numbers
/*for(int i = 0; i < 100; ++i){
int number;
mq.receive(&number, sizeof(number), recvd_size, priority);
if(number != i || recvd_size != sizeof(number))
return 1;
else
std::cout << "nnumber = " << number;
}*/
//Receive Structure
st_Request dataone, datatwo;
mq.receive(&dataone, sizeof(st_Request), recvd_size, priority);
if(recvd_size != sizeof(st_Request))
return 1;
else{
std::cout << "nsrcip = " << dataone.src_ip;
std::cout << "ndesip = " << dataone.des_ip;
std::cout << "nRequest = " << dataone.objRequest.to_string();
std::cout << "nname = " << dataone.name;
}
}
catch(interprocess_exception &ex){
message_queue::remove("message_queue");
std::cout << ex.what() << std::endl;
return 1;
}
message_queue::remove("message_queue");
return 0;
}
// InterProcess Message Queue with Boost::interprocess
// Message Queue Receiver
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ mq_receiver.cpp -o mq_receiver -I path_to_boost -lboost_system-gcc43-mt-1_39
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
using namespace boost::interprocess;
// Structure to be read
typedef struct s_Request{
int src_ip,des_ip;
boost::asio::ip::address objRequest;
char name[50];
}st_Request;
int main ()
{
try{
//Open a message queue.
message_queue mq
(open_only //only create
,"message_queue" //name
);
unsigned int priority;
std::size_t recvd_size;
//Receive 100 numbers
/*for(int i = 0; i < 100; ++i){
int number;
mq.receive(&number, sizeof(number), recvd_size, priority);
if(number != i || recvd_size != sizeof(number))
return 1;
else
std::cout << "nnumber = " << number;
}*/
//Receive Structure
st_Request dataone, datatwo;
mq.receive(&dataone, sizeof(st_Request), recvd_size, priority);
if(recvd_size != sizeof(st_Request))
return 1;
else{
std::cout << "nsrcip = " << dataone.src_ip;
std::cout << "ndesip = " << dataone.des_ip;
std::cout << "nRequest = " << dataone.objRequest.to_string();
std::cout << "nname = " << dataone.name;
}
}
catch(interprocess_exception &ex){
message_queue::remove("message_queue");
std::cout << ex.what() << std::endl;
return 1;
}
message_queue::remove("message_queue");
return 0;
}
Boost Message Queue (Interprocess) Sender
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// InterProcess Message Queue with Boost::interprocess
// Message Queue Sender
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ mq_sender.cpp -o mq_sender -I path_to_boost -lboost_system-gcc43-mt-1_39
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
using namespace boost::interprocess;
// Any structure to be send to message queue
typedef struct s_Request{
int src_ip,des_ip;
boost::asio::ip::address clientAddress;
char name[50];
}st_Request;
int main ()
{
try{
//Create data to be entered
st_Request data1 = {10,20,boost::asio::ip::address::from_string("10.31.141.45"),"www.google.com"};
st_Request data2 = {40,50,boost::asio::ip::address::from_string("192.168.64.152") ,"www.rediff.com"};
//Erase previous message queue
message_queue::remove("message_queue");
//Create a message_queue.
message_queue mq
(create_only //only create
,"message_queue" //name
,100 //max message number
,sizeof(st_Request) //max message size
);
//Send 100 numbers
//for(int i = 0; i < 100; ++i){
// mq.send(&i, sizeof(i), 0);
//}
//Send 2 structures
mq.send(&data1, sizeof(st_Request), 0);
mq.send(&data2, sizeof(st_Request), 0);
}
catch(interprocess_exception &ex){
std::cout << ex.what() << std::endl;
return 1;
}
return 0;
}
// InterProcess Message Queue with Boost::interprocess
// Message Queue Sender
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ mq_sender.cpp -o mq_sender -I path_to_boost -lboost_system-gcc43-mt-1_39
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <boost/interprocess/ipc/message_queue.hpp>
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
using namespace boost::interprocess;
// Any structure to be send to message queue
typedef struct s_Request{
int src_ip,des_ip;
boost::asio::ip::address clientAddress;
char name[50];
}st_Request;
int main ()
{
try{
//Create data to be entered
st_Request data1 = {10,20,boost::asio::ip::address::from_string("10.31.141.45"),"www.google.com"};
st_Request data2 = {40,50,boost::asio::ip::address::from_string("192.168.64.152") ,"www.rediff.com"};
//Erase previous message queue
message_queue::remove("message_queue");
//Create a message_queue.
message_queue mq
(create_only //only create
,"message_queue" //name
,100 //max message number
,sizeof(st_Request) //max message size
);
//Send 100 numbers
//for(int i = 0; i < 100; ++i){
// mq.send(&i, sizeof(i), 0);
//}
//Send 2 structures
mq.send(&data1, sizeof(st_Request), 0);
mq.send(&data2, sizeof(st_Request), 0);
}
catch(interprocess_exception &ex){
std::cout << ex.what() << std::endl;
return 1;
}
return 0;
}
Boost timer
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Boost asynchronous timer.
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ timer.cpp -o timer -I path_to_boost -lboost_system-gcc43-mt-1_39
// Execute: ./timer
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(3));
t.async_wait(print);
io.run();
return 0;
}
// Boost asynchronous timer.
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ timer.cpp -o timer -I path_to_boost -lboost_system-gcc43-mt-1_39
// Execute: ./timer
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Hello, world!n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(3));
t.async_wait(print);
io.run();
return 0;
}
UDP Listener/Receiver with libevent
UDP Listener/Receiver with libevent
Timer with lib event
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// The program listens on multiple udp sockets simultaneously
// and calling the event on them.
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ event_test.c -o event_test -levent
// Execture: ./event_test 0 10
// Listen on 10 udp ports starting from 6000 to 6009.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#define UDP_PORT 6000
#include <iostream>
#include <event.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int g_count = 0;
struct timeval stTv;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// TIMER FUNCTION
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void timer_function(int x, short int y, void *pargs)
{
printf("nn**********Count is %dnn",g_count);
g_count = 0 ;
event_add((struct event*)pargs,&stTv);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Event Function
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void func_for_eve1(int x, short int y, void *pargs)
{
unsigned int unFromAddrLen;
int nByte = 0;
char aReqBuffer[512];
struct sockaddr_in stFromAddr;
unFromAddrLen = sizeof(stFromAddr);
if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0,
(struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1)
{
printf("error occured while receivingn");
}
//printf("Function called buffer is %sn",aReqBuffer);
g_count++;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// MAIN
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int main(int argc, char **argv)
{
struct event_base *base ;
struct event g_Timer;
int val1,val2;
if(argc == 3)
{
val1 = atoi(argv[1]);
val2 = atoi(argv[2]);
printf("Value is %d %dn",val1,val2);
}
else return 0;
struct event g_eve[val2-val1];
int udpsock_fd[val2-val1];
struct sockaddr_in stAddr[val2-val1];
base = event_init();
for(int i=0; i< val2-val1 ; i++)
{
if ((udpsock_fd[i] = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("ERROR - unable to create socket:n");
exit(-1);
}
//Start : Set flags in non-blocking mode
int nReqFlags = fcntl(udpsock_fd[i], F_GETFL, 0);
if (nReqFlags< 0)
{
printf("ERROR - cannot set socket options");
}
if (fcntl(udpsock_fd[i], F_SETFL, nReqFlags | O_NONBLOCK) < 0)
{
printf("ERROR - cannot set socket options");
}
// End: Set flags in non-blocking mode
memset(&stAddr[i], 0, sizeof(struct sockaddr_in));
//stAddr.sin_addr.s_addr = inet_addr("192.168.64.1555552");
stAddr[i].sin_addr.s_addr = INADDR_ANY; //listening on local ip
stAddr[i].sin_port = htons(UDP_PORT+i+val1);
stAddr[i].sin_family = AF_INET;
int nOptVal = 1;
if (setsockopt(udpsock_fd[i], SOL_SOCKET, SO_REUSEADDR,
(const void *)&nOptVal, sizeof(nOptVal)))
{
printf("ERROR - socketOptions: Error at Setsockopt");
}
if (bind(udpsock_fd[i], (struct sockaddr *)&stAddr[i], sizeof(stAddr[i])) != 0)
{
printf("Error: Unable to bind the default IP n");
exit(-1);
}
event_set(&g_eve[i], udpsock_fd[i], EV_READ | EV_PERSIST, func_for_eve1, &g_eve[i]);
event_add(&g_eve[i], NULL);
}
/////////////TIMER START///////////////////////////////////
stTv.tv_sec = 3;
stTv.tv_usec = 0;
event_set(&g_Timer, -1, EV_TIMEOUT , timer_function, &g_Timer);
event_add(&g_Timer, &stTv);
////////////TIMER END/////////////////////////////////////
event_base_dispatch(base);
return 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Timer with lib event
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// The program listens on multiple udp sockets simultaneously
// and calling the event on them.
// Date : 26 - Nov -2009
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
// Compile: g++ event_test.c -o event_test -levent
// Execture: ./event_test 0 10
// Listen on 10 udp ports starting from 6000 to 6009.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#define UDP_PORT 6000
#include <iostream>
#include <event.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int g_count = 0;
struct timeval stTv;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// TIMER FUNCTION
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void timer_function(int x, short int y, void *pargs)
{
printf("nn**********Count is %dnn",g_count);
g_count = 0 ;
event_add((struct event*)pargs,&stTv);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Event Function
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
void func_for_eve1(int x, short int y, void *pargs)
{
unsigned int unFromAddrLen;
int nByte = 0;
char aReqBuffer[512];
struct sockaddr_in stFromAddr;
unFromAddrLen = sizeof(stFromAddr);
if ((nByte = recvfrom(x, aReqBuffer, sizeof(aReqBuffer), 0,
(struct sockaddr *)&stFromAddr, &unFromAddrLen)) == -1)
{
printf("error occured while receivingn");
}
//printf("Function called buffer is %sn",aReqBuffer);
g_count++;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// MAIN
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
int main(int argc, char **argv)
{
struct event_base *base ;
struct event g_Timer;
int val1,val2;
if(argc == 3)
{
val1 = atoi(argv[1]);
val2 = atoi(argv[2]);
printf("Value is %d %dn",val1,val2);
}
else return 0;
struct event g_eve[val2-val1];
int udpsock_fd[val2-val1];
struct sockaddr_in stAddr[val2-val1];
base = event_init();
for(int i=0; i< val2-val1 ; i++)
{
if ((udpsock_fd[i] = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("ERROR - unable to create socket:n");
exit(-1);
}
//Start : Set flags in non-blocking mode
int nReqFlags = fcntl(udpsock_fd[i], F_GETFL, 0);
if (nReqFlags< 0)
{
printf("ERROR - cannot set socket options");
}
if (fcntl(udpsock_fd[i], F_SETFL, nReqFlags | O_NONBLOCK) < 0)
{
printf("ERROR - cannot set socket options");
}
// End: Set flags in non-blocking mode
memset(&stAddr[i], 0, sizeof(struct sockaddr_in));
//stAddr.sin_addr.s_addr = inet_addr("192.168.64.1555552");
stAddr[i].sin_addr.s_addr = INADDR_ANY; //listening on local ip
stAddr[i].sin_port = htons(UDP_PORT+i+val1);
stAddr[i].sin_family = AF_INET;
int nOptVal = 1;
if (setsockopt(udpsock_fd[i], SOL_SOCKET, SO_REUSEADDR,
(const void *)&nOptVal, sizeof(nOptVal)))
{
printf("ERROR - socketOptions: Error at Setsockopt");
}
if (bind(udpsock_fd[i], (struct sockaddr *)&stAddr[i], sizeof(stAddr[i])) != 0)
{
printf("Error: Unable to bind the default IP n");
exit(-1);
}
event_set(&g_eve[i], udpsock_fd[i], EV_READ | EV_PERSIST, func_for_eve1, &g_eve[i]);
event_add(&g_eve[i], NULL);
}
/////////////TIMER START///////////////////////////////////
stTv.tv_sec = 3;
stTv.tv_usec = 0;
event_set(&g_Timer, -1, EV_TIMEOUT , timer_function, &g_Timer);
event_add(&g_Timer, &stTv);
////////////TIMER END/////////////////////////////////////
event_base_dispatch(base);
return 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Subscribe to:
Posts (Atom)