Wednesday 16 November 2011

Search and Replace in awk

ls -R | gawk '{if($1 ~ /.*:$/){split($1,a,"/");if(a[length(a)] ~ /[a-z]/){if(length($1) > 10){print $1;}}}}' | awk '{ sub(/[:]$/, ""); print }' | xargs rm -rf

Wednesday 12 October 2011

Perl Execution from Command Line

Here the test is the file has contents delimited by '|' PIPE , you can split them and do process w.r.t fields


Ex:
perl -e 'while(<>) {@fields=split(/\|/,$_);print "$fields[10]\n";$ctype{$fields[14]}+=$fields[10];} foreach $c (keys %ctype){print "$c:$ctype{$c}\n"}' < test

Thursday 29 September 2011

TCP EchoServer

/*
  @filename : client.c
  @brief: Client Program
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

#define MAX_LINE  256


void *SendReceive(void* sockid);

int main(int argc,char *argv[])
{
  struct sockaddr_in  serv_addr ;
  int           sockid,  tcp_port ;
  pthread_t     th;

  if(argc < 3 )
  {
    printf("usage: %s <server-address> <port> \n",argv[0]);
    return -1;
  }
  tcp_port = atoi(argv[2]) ;

  sockid = socket(AF_INET,SOCK_STREAM,0) ;
  if(sockid == -1 )
  {
    printf("Failed to get socket\n");
    return -2;
  }

  serv_addr.sin_family = AF_INET ;
  serv_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
  serv_addr.sin_port = htons(tcp_port) ;

  if(connect(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) != 0)
  {
    printf("Failed to Connect Server %s\n",argv[2]);
    return -3 ;
  }

  if (pthread_create (&th, NULL, SendReceive, (void*)sockid) != 0)
  {
    close(sockid) ;
    return -4 ;
  }

  pthread_join(th,NULL);
  close(sockid);

  return 0 ;
}

void *SendReceive(void* sockfd)
{

  char  buf[MAX_LINE],sendbuf[MAX_LINE];
  int   length,ret,sockid ;

  sockid =(int)sockfd;
  while(1)
  {
          memset(buf,0,sizeof(buf));
          if(fgets(buf,MAX_LINE,stdin) == NULL )
          {
                  printf ("received EOF from stdin\n");
                  break;
          }

          length = strlen(buf);

          if( (ret = send(sockid, &buf, length, 0)) <= 0 )
          {
                 printf("Client coultn't send message to server\n");
                  break;
          }

          ret = recv(sockid,&buf,MAX_LINE,0);
          if ( ret == 0 )
          {
                  break;
          }
          else if ( ret < 0 )
          {
                  printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
                  break;
          }
          printf("%s",buf);
  }

  return NULL;
}



/*
@filename : server.c
@brief:  Concurrent  Server Program
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>


#define MAX_LINE 256

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
int         conn_count=0;
void *HandlingClient(void *);

int main(int argc,char *argv[])
{
  struct sockaddr_in  serv_addr,  client_addr;
  int           sockid, newsockid, tcp_port, len;
  pthread_t     tid;

  if(argc < 2 )
  {
    printf("usage: %s <port>\n", argv[0]);
    return -1;
  }
  tcp_port = atoi(argv[1]);

  sockid = socket(AF_INET,SOCK_STREAM,0);
  if(sockid < 0 )
  {
    printf("Failed to get socket\n");
    return -1;
  }
 len=1;
  setsockopt (sockid, SOL_SOCKET, SO_REUSEADDR, &len, sizeof(int));

  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(tcp_port);
  if((bind(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) != 0)
  {
    printf("Failed to bind\n");
    return -2;
  }

  pthread_mutex_init(&mutex, NULL);

  listen(sockid,128);

  while (1)
  {
    printf("active connections #%d\n", conn_count);

    len = sizeof(client_addr);
    if((newsockid = accept(sockid,(struct sockaddr*)&client_addr,&len)) == -1)
    {
       printf("Failed to accept the client request\n");
       continue;
    }

    if(pthread_create(&tid, NULL, HandlingClient, (void*)newsockid) != 0 )
    {
      printf("Failed to create thread\n");
      close (newsockid);
      sleep (1);
      continue;
    }

    pthread_mutex_lock(&mutex);
    conn_count ++;
    pthread_mutex_unlock(&mutex);
  }

  pthread_mutex_destroy(&mutex);
  return 0;
}
void *HandlingClient(void *id)
{
  char buf[MAX_LINE];
  int ret, sockid;

  sockid = (int)id;


  while(1)
  {

    ret = recv(sockid,&buf,MAX_LINE,0);
    if ( ret == 0 )
    {
      break;
    }
    else if ( ret < 0 )
    {
      printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
      break;
    }

    if(!strncasecmp(buf,"bye",3))
    {
            pthread_mutex_lock(&mutex);
            conn_count--;
            pthread_mutex_unlock(&mutex);
            break;
    }

    /* send back to client*/
    if( (ret = send(sockid, &buf, ret, 0)) <= 0 )
    {
      printf("Client coultn't send message to server\n");
      break;
    }
  }

  close(sockid);

  return NULL;
}










Makefile:

MESSAGE="Echo Program Built Successfully"
MESSAGE1 = " ------------------------- "

CC      = gcc

CFLAGS  = -g -I./include

all:  server client
    @echo $(MESSAGE1)
    @echo $(MESSAGE)
    @echo $(MESSAGE1)

server: server.o
      ${CC} -o $@ server.o -lpthread

client: client.o
      ${CC} -o $@ client.o -lpthread

clean:
    -@rm *.o

distclean:  clean
    -@rm server client


Wednesday 28 September 2011

Difference between select and poll

/*
@file name : poll.c
@brief: poll for data in to stdin
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/poll.h>

#define MAX 100
#define TO_MS(val) (val*1000)

int main()
{
        char      line[MAX];
        int       ret;
        struct pollfd   fd[1];

        while(1)
        {

                fd[0].fd = 0;
                fd[0].events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL ;

                ret = poll(fd,1,TO_MS(6));
                printf("poll ret[%d]\n",ret);
                if ( ret > 0 && fd[0].revents)
                {
                        printf("event occur\n");
                }
                if(ret < 0 )
                {
                        printf("error [#%d,%s]\n",errno,strerror(errno));
                        return -1;
                }
                if(ret == 0)
                {
                        printf("time out\n");
                        return 0;
                }
                fgets(line,MAX,stdin);
                printf("%s",line);
        }
        return 0;
}



/*
@file name : select.c
@brief: select for data in to stdin
*/


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

#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>


#define MAX 100
#define TO_MS(val) (val*1000)

int main()
{
        char      line[MAX];
        int       ret,fd=0;
        fd_set readfds;
        struct timeval timeout;

        while(1)
        {
                /* set the time out */
                timeout.tv_sec = 2;
                timeout.tv_usec = 0 ;

                /* initialize the fd set*/
                FD_ZERO(&readfds);

                /*set the action to the fd - here stdin*/
                FD_SET(0, &readfds);


                ret = select(fd + 1, &readfds, NULL, NULL, &timeout);
                if (ret == 0)
                {
                  /* timed out, return */
                        printf("Time out happend.!!\n");
                        break ;
                }
                else if (ret < 0)
                {
                        /* there is an error, return */
                        printf("Error Occurs Err[#%d,%s]\n",errno,strerror(errno));
                        break ;
                }
                else
                {
                        if (FD_ISSET (0, &readfds))
                        {
                            printf("data in to read\n");
                        }
                        else
                        {
                          printf("Strange..!! No Data available after fd set\n");
                          continue;
                        }
                }

                fgets(line,MAX,stdin);
                printf("%s",line);
        }

      return 0;
}