Featured Post

Wednesday, October 5, 2016

UNIX Programming Lab Manual

                      


/* Write a C program to copy one file to another file */

#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
void main()
{
   int fd1,fd2;  
   char buf[1024];
   long int n=0;
   if((fd1=open("sample1",O_RDONLY))==-1)  /* open sample file read only mode */
   {
      printf("source file doesnt exist"); 
   }
   if((fd2=open("sample2",O_CREAT|O_WRONLY,S_IRWXU))==-1)
  {
      printf("problem in creating the target file");
   }
   while((n=read(fd1,buf,5))>0)   /* reading maximum five characters at time*/
   { 
      write(fd2,buf,n);   /* Writing read characters into array buf */
   }
}



Output:
/home/ashok/~  cat>sample1
hi hello
^d

/home/ashok/~ cc copy.c

/home/ashok/~ ./a.out

/home/ashok/~ cat sample2

hi hello





/*Write a C program to count no of characters in a given file?*/


#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
void main()
{
     int fd1,l=0;
     char buf[1024];
     long int n;
     if((fd1=open("sample1",O_RDONLY))<0) /* open sample1 file in read only mode */
     {
        printf("error opening a file");
     }
     while((n=read(fd1,buf,1))>0) /* reading maximum one character at a time */
      {
        l++;
      }
     printf("number of characters in file %d",l);
      close(fd1);
}


Output:
/home/ashok/~ cc count.c
/home/ashok/~ ./a.out
number of characters in file 9


  



/* Write a C program which gives files meta data by using stat system call?*/

#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
void main()
{
int fd;
struct stat x;
fd=open("copy.c",O_RDONLY); /* opens the file copy.c in read only mode */
            if(fd<0)
           {
                 printf("error");
            }
if(fstat(fd,&x)<0)  /* stat system call is used to get the meta data about file */
printf("error displaying metadata");
printf("SIZE %d \n node: %d\n device_id: %d \n protection: %d\n block for file system I/O:%d\n",x.st_size,x.st_ino,x.st_dev,x.st_mode,x.st_blksize);
printf("userid: %d\n group id: %d \n No.of hardlinks: %d\n no.of blocks allocated:%d\n time of last access: %d\n time of last modification: %d\n time of last status change %d\n",x.st_uid,x.st_gid,x.st_nlink,x.st_blocks,x.st_atime,x.st_mtime,x.st_ctime);

close(fd);
}

Output:
/home/ashok/~ cc metadata.c
SIZE 371
 node: 7866036
 device_id: 64770
 protection: 33204
 block for file system I/O:4096
userid: 509
 group id: 509
 No.of hardlinks: 1
 no.of blocks allocated:8
 time of last access: 1475515571
 time of last modification: 1475515571
 time of last status change 1475515571



/* Write a C program which reads a set of file names along the command line and then prints them according to their sizes?*/

#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>

int  main(int argc,char *argv[])
{
struct stat x;
int i,j;
int *size,t;
char *tmp;
size=(int *)malloc((argc-1)*sizeof(int));
for(i=1;i<argc;i++)
{
if(stat(argv[i],&x)<0)
{
printf("%s\n", argv[i]);
exit(-1);
}
size[i-1]=x.st_size;
/*printf("\n %s %ld\n",argv[i],x.st_size);*/
}
for(i=0;i<argc-2;i++)
{
            for(j=0;j<argc-2;j++)
{
  if(size[j]<size[j+1])
   {
      t=size[j];
      size[j]=size[j+1];
      size[j+1]=t;
      tmp=argv[j+1];
      argv[j+1]=argv[j+2];
      argv[j+2]=tmp;
    }
                         }



}


for(i=0;i<argc-1;i++)
printf("%s %d\n",argv[i+1],size[i]);
}

Output:

/home/ashok/~ cc filedata.c
/home/ashok/~ ./a.out hello
./a.out hello
hello 16



/* Write a C program to create a process tree? */

#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
void main()
{
 fork(); /* fork() system call is used to create child process */
 fork();
 fork();
 printf("Hello\n");
}

Output:

/home/ashok/~ cc processtree.c
/home/ashok/~ ./a.out
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello


/* Write  a C program to create process chain i.e.,  every parent has one child*/

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>

void main()
{
 if(fork()==0)          /*  fork() system call is used to create child process */
 {
   printf("Pid=%d\tPPid=%d\n",getpid(),getppid());
    if(fork()==0)      /* child process */
    {
     printf("Pid=%d\tPPid=%d\n",getpid(),getppid());
      if(fork()==0)
      {
        printf("Pid=%d\tPPid=%d\n",getpid(),getppid());

       }

    }

 }
}

Output:
/home/ashok/~ cc processchain.c
/home/ashok/~ ./a.out

Pid=5274        PPid=5273
Pid=5275        PPid=1
Pid=5276   PPid=1


/* Write a C program to create orphan’s process? */

#include<stdio.h>
#include<unistd.h>
void main()
{
   int pid;
   pid=fork();   /* fork() system call is used to create child process */
   if(pid==0)
   {
        printf(“\n child process id is %d \t parent process id is %d”,getpid(),getppid());
        sleep(10);
         printf(“\n child process id is %d \t parent process id is %d”,getpid(),getppid());
    }
}

Output:

/home/ashok/~ cc orphanprocess.c
/home/ashok/~ ./a.out

child process id is 2322   parent process id 2345

child process id is 2322    parent process id is 1



/* Write a C program to demonstrate the use of execl() system call */

#include<stdio.h>
#include<unistd.h>
void main()
{
        printf("\nBefore execl() function");
        execl("/bin/ls","ls","-l",NULL);    /* replace present process address space with ls 
                                                                      code*/
        printf("\n after execl() function\n");
}

Output:
/home/ashok/~ cc execldemo.c
/home/ashok/~ ./a.out

total 96
-rwxrwxr-x. 1 ashok ashok 6702 Oct  4 22:49 a.out
-rw-rw-r--. 1 ashok ashok  478 Jul 26 22:22 copy.c
-rw-rw-r--. 1 ashok ashok  416 Jul 12 23:16 count.c
-rw-rw-r--. 1 ashok ashok  220 Aug 30 23:16 directory.c
-rw-rw-r--. 1 ashok ashok  171 Aug  2 22:28 environ.c
-rw-rw-r--. 1 ashok ashok  167 Oct  4 22:36 execl.c
-rw-rw-r--. 1 ashok ashok  187 Jul 26 23:41 execlp.c
-rw-rw-r--. 1 ashok ashok  197 Oct  4 22:48 execv.c
-rw-rw-r--. 1 ashok ashok  205 Aug 30 23:15 execvp.c
-rw-rw-r--. 1 ashok ashok   13 Jul 12 22:26 hello
-rwx------. 1 ashok ashok   13 Jul 19 22:20 hihello
-rw-rw-r--. 1 ashok ashok  487 Aug 30 23:52 ipcpipefork.c
-rw-rw-r--. 1 ashok ashok  369 Aug 30 23:47 ipcpipes.c
-rw-rw-r--. 1 ashok ashok  390 Jul 19 22:28 meta.c
-rw-rw-r--. 1 ashok ashok  655 Sep 20 22:50 msgque.c
drwxrwxr-x. 2 ashok ashok 4096 Aug  3 00:00 mydir
drwxrwxr-x. 2 ashok ashok 4096 Aug  3 00:14 mydirr
-rw-rw-r--. 1 ashok ashok  357 Jul 20 00:01 processchain.c
-rw-rw-r--. 1 ashok ashok  147 Jul 20 00:12 processtree.c
……………………….
……………………….


/* Write a C program to demonstrate the use of execv() system call */

#include<stdio.h>
#include<unistd.h>
void main()
{
        char *a[3];
        a[0]="ls";
        a[1]="-l";
        a[2]=0;
        printf("\n Before execv() function");
        execv("/bin/ls",a);  );    /* replace present process address space with ls 
                                                                      code*/
        printf("\nAfter execv() function");
}

Output:
home/ashok/~ cc execvdemo.c
/home/btecj3/~ ./a.out

total 96
-rwxrwxr-x. 1 ashok ashok 6702 Oct  4 22:49 a.out
-rw-rw-r--. 1 ashok ashok  478 Jul 26 22:22 copy.c
-rw-rw-r--. 1 ashok ashok  416 Jul 12 23:16 count.c
-rw-rw-r--. 1 ashok ashok  220 Aug 30 23:16 directory.c
-rw-rw-r--. 1 ashok ashok  171 Aug  2 22:28 environ.c
-rw-rw-r--. 1 ashok ashok  167 Oct  4 22:36 execl.c
-rw-rw-r--. 1 ashok ashok  187 Jul 26 23:41 execlp.c
-rw-rw-r--. 1 ashok ashok  197 Oct  4 22:48 execv.c
-rw-rw-r--. 1 ashok ashok  205 Aug 30 23:15 execvp.c
-rw-rw-r--. 1 ashok ashok   13 Jul 12 22:26 hello
-rwx------. 1 ashok ashok   13 Jul 19 22:20 hihello
-rw-rw-r--. 1 ashok ashok  487 Aug 30 23:52 ipcpipefork.c
-rw-rw-r--. 1 ashok ashok  369 Aug 30 23:47 ipcpipes.c
-rw-rw-r--. 1 ashok ashok  390 Jul 19 22:28 meta.c
-rw-rw-r--. 1 ashok ashok  655 Sep 20 22:50 msgque.c
drwxrwxr-x. 2 ashok ashok 4096 Aug  3 00:00 mydir
………………………..

………………………….
…………………..                                                          

/* Write a C program to demonstrate the use of execlp() system call */

#include<stdio.h>
#include<unistd.h>
void main()
{
        printf("\n Before execlp() function");
        execlp("ls","ls","-l",NULL);  );    /* replace present process address space with ls 
                                                                      code*/
        printf("\n After execlp() function");
}

output:
home/ashok/~ cc execlpdemo.c
/home/btecj3/~ ./a.out

               total 96
-rwxrwxr-x. 1 ashok ashok 6680 Oct  4 22:37 a.out
-rw-rw-r--. 1 ashok ashok  478 Jul 26 22:22 copy.c
-rw-rw-r--. 1 ashok ashok  416 Jul 12 23:16 count.c
-rw-rw-r--. 1 ashok ashok  220 Aug 30 23:16 directory.c
-rw-rw-r--. 1 ashok ashok  171 Aug  2 22:28 environ.c
-rw-rw-r--. 1 ashok ashok  167 Oct  4 22:36 execl.c
-rw-rw-r--. 1 ashok ashok  187 Jul 26 23:41 execlp.c
-rw-rw-r--. 1 ashok ashok  197 Jul 26 23:45 execv.c
-rw-rw-r--. 1 ashok ashok  205 Aug 30 23:15 execvp.c
-rw-rw-r--. 1 ashok ashok   13 Jul 12 22:26 hello
-rwx------. 1 ashok ashok   13 Jul 19 22:20 hihello
-rw-rw-r--. 1 ashok ashok  487 Aug 30 23:52 ipcpipefork.c
-rw-rw-r--. 1 ashok ashok  369 Aug 30 23:47 ipcpipes.c
-rw-rw-r--. 1 ashok ashok  390 Jul 19 22:28 meta.c
-rw-rw-r--. 1 ashok ashok  655 Sep 20 22:50 msgque.c
drwxrwxr-x. 2 ashok ashok 4096 Aug  3 00:00 mydir
…………………………………………………………
…………………………………………………………..
………………………………………………………..


/* Write a C program to demonstrate the use of execv() system call */

#include<stdio.h>
#include<unistd.h>
void main()
{
  char *c[3];
  c[0]="ls";
  c[1]=NULL;
  c[2]=0;
  printf("\nBefore execv system call");
  execvp("ls",c); );    /* replace present process address space with ls 
                                                                      code*/
  printf("\nAfter execv system call");
}

output:
home/ashok/~ cc execvpdemo.c
/home/btecj3/~ ./a.out


a.out        environ.c  execvp.c       ipcpipes.c  mydirr          shmclient.c
copy.c       execl.c    hello          meta.c      processchain.c  shmserver.c
count.c      execlp.c  hihello       msgque.c   processtree.c    size.c
directory.c    execv.c   ipcpipefork.c       mydir     receive.c


………………………………………………………………………………………………..……………………………………………………………………………………………………………………………………………………………………………………………


/* Write a C program to print environment variables */

#include <stdio.h>

void main(int argc, char *argv[], char * envp[])
{
    int i;

    for (i = 0; envp[i] != NULL; i++)       /* enpv[] is a environment variable array which
                                                                   contains all environment variables  */
    {   
        printf("\n%s", envp[i]);
    }
}



Output:
home/ashok/~ cc envdemo.c
/home/btecj3/~ ./a.out


HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=10.50.12.61 49255 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
OLDPWD=/home/ashok
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/30
USER=ashok
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………


/*Write a C program where a child process inherits all the environment variables of a parent process?*/

#include<stdio.h>
#include<stdio.h>
#include<unistd.h>
extern char **environ;
void main(int argc,char *argv[])
{
fork();
int i;
for(i=0;environ[i];i++)      );    /* replace present process address space with ls 
                                                                      code*/
printf("%s\n",environ[i]);
}

Output:-
home/ashok/~ cc childenv.c
/home/btecj3/~ ./a.out


HOSTNAME=localhost.localdomain
SELINUX_ROLE_REQUESTED=
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=10.50.12.167 49209 22
SELINUX_USE_CURRENT_RANGE=
QTDIR=/usr/lib64/qt-3.3
OLDPWD=/home/ashok
QTINC=/usr/lib64/qt-3.3/include
SSH_TTY=/dev/pts/2
USER=ashok
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………


/* Write a C program to implement inter process communication using pipes */

#include<stdio.h>
#include<errno.h>
#include<unistd.h>
int main()
{
        char buf[20];
        int pfds[2];
        if(pipe(pfds)==-1)  /* pipe system call takes an array of two size one for open end of
                                           pipe and the other for closed end of the pipe */
        {
                perror("pipes");
                exit(0);
        }
        printf("\nWriting the file descripter %d",pfds[1]);
        write(pfds[1],"ashok",6); /* writes string ashok to write end of the pipe */
        printf("\nReading the file descripter %d",pfds[0]);
        read(pfds[0],buf,6);    /* reads string ashok in to character array buf */
        printf("Read data is %s",buf);
}
OUTPUT:
home/ashok/~ cc pipesdemo.c
/home/btecj3/~ ./a.out

Writing the file descriptor 4
Reading the file descriptor 3
The data is ashok


/* Write a C program to implement producer consumer problem using named pipes */.


/*producer.c*/
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<stdlib.h>
#define FIFO_NAME "XYZ"
void main()
{
  int num,fd;
   char s[300];
   mknod(FIFO_NAME,S_IFIFO|0666,0); /*named pipes creation whose name is XYZ*/        
    printf("waiting for readers\n");
   fd=open(FIFO_NAME,O_WRONLY); /* blocks until reader is ready to accept data */
   printf("got a reader type some stuff");
   scanf("%s",&s);

     if((num=write(fd,s,strlen(s)))==-1)
       perror("write");
     else
     printf("speak:wrote %d bytes\n", num);


}
Output:
home/ashok/~ cc producer.c
/home/btecj3/~ ./a.out

Waiting for reader
Got a reader type some stuff  hello
Speak:wrote:5


/*consumer.c*/
#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#include<errno.h>
#define FIFO_NAME "XYZ"
void main()
{
  char s[300];
    int num,fd;
  mknod(FIFO_NAME,S_IFIFO|0666,0); /*named pipes creation whose name is XYZ*/        
    printf("waiting for writers\n");
    fd=open(FIFO_NAME,O_RDONLY); /* blocks until the writer is ready to read */
    printf("got a writer\n");
    do
     {
       if((num=read(fd,s,300))==-1)
             perror("read");
        else
         {
             s[num]='\0';
             printf("read %s ",s);
         }
     }while(num>0);
}
output:
home/ashok/~ cc consumer.c
/home/btecj3/~ ./a.out

waiting for writers
got a writer

hello


/* Write a C program to illustrate inter process communication using share memory */

/*shmserver.c:*/

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
# define MAXSIZE 27
int main()
{
        char c;
        int shmid;
        key_t key;
        char *shm,*s;
        key=557;
        if((shmid=shmget(key,MAXSIZE,IPC_CREAT|0666))<0)  
             {         /*creation of shared memory segment with key value key */
                     perror(s);
                     exit(1);
              }

        if((shm=shmat(shmid,NULL,0))==(char*)-1)
               {          /* attaching process address space to shared memory address */
                     perror(“shmat”);
                     exit(1);
                }
        /* * put something into the memory for the other process to read * */
        s=shm;
        for(c='a';c<='z';c++)
                *s++=c;
        while(*shm!='*')
                sleep(1);
        exit(0);
}
Output:
home/ashok/~ cc shmserver.c
/home/btecj3/~ ./a.out


/*shmclient.c*/

#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<stdio.h>
#include<stdlib.h>
# define MAXSIZE 27
int main()
{
        char c;
        int shmid;
        key_t key;
        char *shm,*s;
        key=557;
        if((shmid=shmget(key,MAXSIZE,IPC_CREAT|0666))<0)
          {                     /*creation of shared memory segment with key value key */

                      perror(s);
                     exit(1);
           }

        if((shm=shmat(shmid,NULL,0))==(char*)-1)
                {  /* attaching process address space to shared memory address */
                               perror(s);
                                 exit(1);
                }

        /* * put something into the memory for the other process to read * */
        s=shm;
        for(s=shm;*s!='\0';s++)
                putchar(*s);
        *shm='*';
        exit(0);
}


Output:
home/ashok/~  cc shmclient.c
/home/btecj3/~ ./a.out

ab c d e f g h i j k l m n o p q r s t u v w x y z


 /*Write a C program to illustrate inter process communication  using  message queues.*/
           
/*msgsnd.c*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE     128


struct msgbuf
{
    long    mtype;
    char    mtext[MAXSIZE];
};
main()
{
    int msqid;
    int msgflg = IPC_CREAT | 0666;
    key_t key;
    struct msgbuf sbuf;
    size_t buflen;

    key = 123555;

    if ((msqid = msgget(key, msgflg )) < 0)   //Get the message queue ID for the given key
     /* creation of message queue with msgflg */

    printf("eorror");

    //Message Type
    sbuf.mtype = 1;   /* send and read messages in sequential order */

    printf("Enter a message to add to message queue : ");
    scanf("%s",sbuf.mtext);
    getchar();

    buflen = strlen(sbuf.mtext) + 1 ;

    if (msgsnd(msqid, &sbuf, buflen, IPC_NOWAIT) < 0)
    {  
             printf("error");
    }

    else
 printf("Message Sent\n");

    exit(0);
}

Output:
home/ashok/~ cc msgsnd.c
/home/btecj3/~ ./a.out

Enter a message to add to message queue:hello
message sent



/*msgclient.c*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE     128


struct msgbuf
{
    long    mtype;
    char    mtext[MAXSIZE];
};
main()
{
    int msqid;
    key_t key;
    struct msgbuf rcvbuffer;

    key = 123555;

    if ((msqid = msgget(key, 0666)) < 0)
                                     /* creating message queue with octal permission 0666*/
       printf("message queue cannot be created");

    /* Receive an answer of message type 1.*/
    if (msgrcv(msqid, &rcvbuffer, MAXSIZE, 1, 0) < 0)
      printf("\n error in reading the message from the message queue");

    printf("%s", rcvbuffer.mtext);
    exit(0);
}




Output:
/home/ashok/~ cc msgclient.c
/home/btecj3/~ ./a.out
hello


/*Write a C program to implement file locking using semaphores?*/
 
/*semlock.c*/

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<sys/ipc.h>
int main()
{
  int sem1;

char a='5';
   sem1=semget(1212,1,IPC_CREAT|0666);/* creating semaphore */
  if(sem1<0)
  printf("not created semaphore");
   semctl(sem1,0,SETVAL,6);/ * setting value to semaphore */
   struct sembuf s1;
   FILE *fd;
    fd=fopen("/home/netsem2014/akr","a");
  
    s1.sem_num=0;
    s1.sem_op=-1;
     s1.sem_flg=0;
   semop(sem1,&s1,1);/* lock applied */     

putc(a,fd);/trying to place data into file akr */
 fclose(fd);

s1.sem_num=0;
s1.sem_op=1;
s1.sem_flg=0;
semop(sem1,&s1,1);/*lock released */
}




/* the program trying to write something to a file akr but it cannot write anything to the file because the file is locked with semaphore at the time of writing. so there is no change in the content of the file */

Output:
/home/ashok/~ cat>akr
Hi hello how are you?


/home/ashok/~ cc semlock.c
/home/ashok/~ ./a.out

/home/ashok/~ cat akr
hi hello how are you

/*Write C program to implement TCP echo program using sockets?*/
/*tcpserver.c*/
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#define MAXLINE 5544
void main(int arg, char *argv[])
{
  int sockfd,connfd,childpid,n;
 unsigned short port;
 int len;
 char mesg[1024];
   struct sockaddr_in servaddr,cliaddr;
   port=(unsigned short)atoi(argv[1]);
   if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
/* creation of datagram socket */
   perror("socket");
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family=AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    servaddr.sin_port=htons(port);
    
    if((bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr)))<0)
    /* biding of socket to socket address */
   perror("bind");
    listen(sockfd,5); / server accepts maximum 5 clients requests at a time */
   for(;;)
   {
     len=sizeof(cliaddr);
     connfd=accept(sockfd,(struct sockaddr *)&cliaddr,&len);
      close(sockfd);*/
     n=write(sockfd,mesg,MAXLINE);
       read(sockfd,mesg,MAXLINE);
  }
}
OUTPUT:
    /home/ashok/~ cc tcpserver.c
    /home/ashok/~ ./a.out 2233

/*tcpclient.c*/
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/types.h>
#define MAXLINE 5544
void main(int argc,char *argv[])
{
  int sockfd,n;
  unsigned short port;
   int len;
    char mesg[1024];
    struct sockaddr_in servaddr;
    port=(unsigned short)atoi(argv[1]);
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
   /* creation of data gram sockets */
    perror("socket");
    bzero(&servaddr,sizeof(servaddr)); /* initialize all socket address structures to zero*/
     servaddr.sin_family=AF_INET;
     inet_pton(AF_INET,argv[2],&servaddr.sin_addr);
      len=sizeof(servaddr);
     connect(sockfd,(struct sockaddr *)&servaddr,len);
    while(fgets(mesg,MAXLINE,stdin)!=NULL)
    {
       write(sockfd,mesg,MAXLINE);
       read(sockfd,mesg,MAXLINE);
      printf("\n %s",mesg);
    }
}
Output: /home/ashok/~ cc tcpclient.c
        /home/ashok/~ ./a.out 2233 127.0.0.1         
       hi
       hi
       hello
       hello

/*Write C program to implement UDP echo program using sockets ?*/
/*udp server.c*/
#include<unistd.h>
#include<sys/socket.h>
#include<stdio.h>
#include<string.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<errno.h>
#define MAXLINE 5555
void main(int argc,char *argv[])
{
  int sockfd,n;
  unsigned short port;
  socklen_t len;
   char mesg[1024];
   struct sockaddr_in servaddr,cliaddr;
   port=(unsigned short)atoi(argv[1]);
   if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) /* creation of sockets*/
   printf("error in socket creation");
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family=AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(port);
   len=sizeof(servaddr);
   if(bind(sockfd,(struct sockaddr *)&servaddr,len)<0) /* binding to socket address */
   perror("\nbind");
  for(;;)   /* infinite loop */
   {
   len=sizeof(cliaddr);
     n=recvfrom(sockfd,&mesg,MAXLINE,0,(struct sockaddr *)&cliaddr,&len);
     sendto(sockfd,&mesg,n,0,(struct sockaddr *)&cliaddr,len);
 
}

output:
/home/ashok/~ cc udpserver.c
/home/ashok/~ ./a.out 4455 





                  /*udpclient.c*/


#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<netinet/in.h>
#include<errno.h>
#define MAXLINE 5566

void main(int argc,char **argv[])
{

  int sockfd,n,len;
   char mesg[1024];
   unsigned short port;
  char sendline[MAXLINE],recvline[MAXLINE];
   struct sockaddr_in servaddr;
   if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0) /* creating sockets */
   {
     perror("socket");
   }
   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family=AF_INET;
   port=(unsigned short)atoi(argv[1]);

   servaddr.sin_port=htons(port);
   if((inet_pton(AF_INET,argv[2],&servaddr.sin_addr))<0)
   {
    perror("inet_pton");
   }
   len=sizeof(servaddr);
  while(fgets(sendline,MAXLINE,stdin)!=NULL)
  {
  
    sendto(sockfd,&sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,len);
   n=recvfrom(sockfd,&recvline,MAXLINE,0,(struct sockaddr *)&servaddr,&len);
   recvline[n]=0;

    printf("\n %s",recvline);
  }
}



output:

/home/ashok/~ cc udpclient.c

/home/ashok/~ ./a.out 4455 127.0.0.1

hi


hi


hello


hello




List of lab programs ( UNIX Programming)
copy one file to another, count no of characters in a given file Write a C program which gives files meta data by using stat system call Write a C program which reads a set of file names along the command line and then prints them according to their size Write a C program to create a process tree Write a C program to create a process chain Write a C program to create orphan’s process Write a C program to demonstrate the use of execl() system call Write a C program to demonstrate the use of execv() system call Write a C program to demonstrate the use of execlp() sysem call Write C program to demonstrate the use of execvp() system call Write a C program to print environment variables W rite a C program where a child process inherits all the environment variables of a parent process Write a C program to illustrate inter process communication using pipes Write a C program to implement producer consumer problem using named pipes Write a C program to illustrate inter process communication using shared memory Write a C program to illustrate inter process communication using message queues Write a C program to implement file locking using semaphores Modi banned 500rs and 1000rs. new 500rs notes, new 1000rs note, new 2000 rupee note, new currency with chip enabled, 2000rs note with chip enabled.logic behind banning,hange, tax exemption on money, tax exemption on toll gate free, ATMS not wroking , firday new notes released ,  why 500 and 1000rs notes banned  abdul kalam photo on 200rs n otes, tax on currency exc500 100rs notes clab sample programs Write a C program to implement one to chat application using TCP sockets Write a C program to implement one to chat application using UDP sockets? inter process communcation, ftok(), advance unix programming lab, netwrok programming lab manual, cse lab manual unix, mca unix programming lab manual, UNIX lab manual, socket programming, client server programming using tcp socets






No comments:

Post a Comment