It's hard to imagine why you wouldn't use Perl's built-in TCP
support. There's an entire O'Reilly book on this topic.
Because if I use a perl program there will be a perl script forked
everytime when called.
Much worse than executing a c binary client program.
This is a part of a mailing system , which receives upto 100k mails an
hour
Currently mail reaches postfix , postfix calls a perl script and does
the processing , looking for particular strings using regex and
But the things are not quick enough.
This is not my original idea

. Like spamc is used as a small tcp
client to talk to spamd for antispam spamassassin scanning.
This is the code
/*
* Most of the code is from
http://www.paulgriffiths.net/program/c/srcs/echoclntsrc.html
*
*/
/* Just included everything possible .. dont think it makes a
difference to the code */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <syslog.h>
#include <netdb.h>
#include <string.h>
#include <ctype.h>
#include <pwd.h>
#include <regex.h>
#include <sys/time.h>
#include <signal.h>
#include <unistd.h>
#include <arpa/inet.h>
#define BOUNCEHOST "127.0.0.1"
#define BOUNCEPORT 1601
#define MAX_LINE 1000
/* Write a line to a socket */
ssize_t Writeline(int sockd, const void *vptr, size_t n) {
size_t nleft;
ssize_t nwritten;
const char *buffer;
buffer = vptr;
nleft = n;
while ( nleft > 0 ) {
if ( (nwritten = write(sockd, buffer, nleft)) <= 0 ) {
if ( errno == EINTR )
nwritten = 0;
else
return -1;
}
nleft -= nwritten;
buffer += nwritten;
}
return n;
}
int main(int argc, char* argv[]) {
int conn_s;
struct sockaddr_in servaddr;
char buffer[MAX_LINE];
if (argc != 3) {
fprintf(stderr, "Incorrect usage\n");
exit(1);
}
if ( (conn_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "ECHOCLNT: Error creating listening socket.\n");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(BOUNCEPORT);
if ( inet_aton(BOUNCEHOST, &servaddr.sin_addr) <= 0 ) {
printf("ECHOCLNT: Invalid remote IP address.\n");
exit(EXIT_FAILURE);
}
if ( connect(conn_s, (struct sockaddr *) &servaddr,
sizeof(servaddr) ) < 0 ) {
printf("ECHOCLNT: Error calling connect()\n");
exit(EXIT_FAILURE);
}
while(fgets(buffer,MAX_LINE,stdin)) Writeline(conn_s, buffer,
strlen(buffer));
exit(0);
}