Monday, December 21, 2009

UDP Sender C Program

A Program which sends udp packet to a defined port PORT.
Set SRV_IP of the receiving server,
Number of Packets = NPACK * 1000 * 1000

#define SRV_IP "127.0.0.1"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>

#define BUFLEN 512
#define NPACK 10
#define PORT 6000

void diep(char *s)
{
perror(s);
exit(1);
}

int main(void)
{
struct sockaddr_in si_other;
long int j;
int fd, slen=sizeof(si_other);
char buf[BUFLEN];

int i;
{
if ((fd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");

memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);

if (inet_aton(SRV_IP, &si_other.sin_addr)==0)
{
fprintf(stderr, "inet_aton() failedn");
exit(1);
}
}

for (j=0; j<NPACK * 1000 * 1000; j++)
{
sprintf(buf, "512 length string you can type here anything this is so and so packetn");
if (sendto(fd, buf, BUFLEN, 0, &si_other, slen)==-1)
diep("sendto()");
}
close(fd);
return 0;
}

strtok Example

Tokenizing at '*' and '/'

#include <stdio.h>
#include <string.h>


int
main() {
char *token;
char *lasts;
char buf[50] ;

strcpy(buf,"3/4/5/**6*7");
printf("ntokenizing %s with strtok_r():n", buf);

if ((token = strtok_r(buf, "*/", &lasts)) != NULL) {
printf("token = %sn", token);
while ((token = strtok_r(NULL, "*/", &lasts)) != NULL) {
printf("token = %sn", token);
}
}
}


OUTPUT:
tokenizing 3/4/5/**6*7 with strtok_r():
token = 3
token = 4
token = 5
token = 6
token = 7

sscanf with expression

Using expression with sscanf

int main()
{
char rangestr = "10.32.125.224 - 220.12.32.52";
char ip1[16], ip2[16];
retval = sscanf(rangestr, "%[^'-'] - %s", ip1,ip2);
printf ("return calue = %d ip1 = %s, ip2 = %s",retval,ip1,ip2);

return 0;
}

%[^'-'] = From '^' start read till '-' character

System V Message queue

System V Message Queue

Message Queue Sender Program
//-----------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

struct my_msgbuf {
long mtype;
char mtext[1000];
//char mtext;
};

int main(void)
{
struct my_msgbuf buf;
int msqid;
key_t key = 2221;

if ((msqid = msgget(key, 0666 | IPC_CREAT)) < 0) {
perror("msgget");
exit(1);
}
printf("nmsgqid %dn", msqid);

buf.mtype = 1;
strcpy(buf.mtext,"hello world ");
size_t len = sizeof(struct my_msgbuf) - sizeof(long);
printf("len %dn", len);

struct msqid_ds buf_ds;
while(1)
{
sleep(2);
if (msgsnd(msqid, &buf, len, IPC_NOWAIT) < 0)
{
// if message queue is full increase the message queue size
if(msgctl(msqid, IPC_STAT, &buf_ds) < 0)
{
perror("msgctl:get");
exit(1);
}

buf_ds.msg_qbytes += 16*1024;
if(msgctl(msqid, IPC_SET, &buf_ds) < 0)
{
perror("msgctl:set");
exit(1);
}

if (msgsnd(msqid, &buf, len, IPC_NOWAIT) < 0)
{
perror("msgsnd");
exit(1);
}
}
}
return 0;
}
//-----------------------------------------------------------------//


Message Queue Receiver Program
//-----------------------------------------------------------------//
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>

#define MSGSZ 1000

/*Declare the message structure*/

typedef struct my_msgbuf {
long mtype;
char mtext[1000];
//char mtext;
} message_buf;

main()
{
int msqid;
key_t key;
message_buf rbuf;

/*
*Get the message queue id for the
* "name" 2221, which was created by
* the server.
*/
key = 2221;

if ((msqid = msgget(key, 0666)) < 0) {
perror("msgget");
return (0);
}

while(1)
{
if (msgrcv(msqid, &rbuf, MSGSZ, 1, 0) < 0) {
perror("msgrcv");
return(0);
}

/*Print the answer*/

printf("%sn", rbuf.mtext);
}
return (0);
}
//-----------------------------------------------------------------//

Syslog Example

Start logging with syslog, system logging utility.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>

int main(void) {
int i;
setlogmask (LOG_UPTO (LOG_NOTICE));
openlog ("exampleprog", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);

for (i = 0;i<20000;i++)
{
syslog (LOG_NOTICE, "Program started by User %d", getuid ());
syslog (LOG_INFO, "A tree falls in a forest");

}
setlogmask (LOG_UPTO (LOG_INFO));
for (i = 0;i<20000;i++)
{
syslog (LOG_NOTICE, "Program started by User %d", getuid ());
syslog (LOG_INFO, "A tree falls in a forest");

}
closelog ();


return 0;
}

Signal Handling

An example of signal handling in C.


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void
termination_handler (int signum)
{
printf("signal called number %dn", signum);
if(signum == SIGUSR1)
signal (SIGUSR1, termination_handler);
//if(signum == SIGINT)
//signal (SIGINT, termination_handler);
if(signum == SIGUSR2)
signal (SIGUSR2, termination_handler);
}

int main (void)
{
{
signal (SIGUSR1, termination_handler);
perror("??");
signal (SIGUSR2, termination_handler);
perror("??");
//signal (SIGINT, termination_handler);
//perror("??");
}

while(1)
{
sleep(100);
printf("");
}

}

STD Merge

A very simple example using merge algorithm. Two arrays merged into a vector.
Merge algorithm requires arrays to be sorted first.


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
vector<int> v(10);
vector<int>::iterator it;

sort (first,first+5);
sort (second,second+5);
merge (first,first+5,second,second+5,v.begin());

cout << "The resulting vector contains:";
for (it=v.begin(); it!=v.end(); ++it)
cout << " " << *it;

cout << endl;

return 0;
}