unix pgms


Ex.No: 1
Date:
BASIC COMMANDS IN UNIX
AIM:
         To write a program to execute the functions of basic commands in unix.

 COMMANDS:
1. who: To view all the working users in your environment.
Syntax: $who
Output:
[maha@localhost ~]$ who
maha     pts/0        Dec 20 00:46 (192.168.0.145)
maha     pts/1        Dec 20 01:04 (192.168.0.144)
maha     pts/2        Dec 20 01:07 (192.168.0.150)
2. who am i: To view current user of the system at particular terminal.
Syntax: $who am i
Output:
[maha@localhost ~]$ who am i
maha     pts/2        Dec 20 01:07 (192.168.0.150)
3. date: To view the system date and time that is shown by internal clock.
Syntax: $date
Output:
[maha@localhost ~]$ date
Tue Dec 20 01:13:24 IST 2011          



4. cal: To view the present year month calander.
Syntax: $cal
Output:
[maha@localhost ~]$ cal
December 2011
Su Mo Tu We Th Fr Sa
                        1   2   3
 4    5    6   7   8    9  10
11  12  13 14  15 16 17
18 19  20  21  22 23 24
25 26  27  28  29 30 31
5. echo:  To display the givem message.
Syntax:$echo “message”
Output:
[maha@localhost ~]$ echo "MAHARAJA ENGINEERING COLLEGE"
MAHARAJA ENGINEERING COLLEGE
6:ls: To list all files inside a directory
Syntax: $ls[option] dir name
Output
[maha@localhost ~]$ls
Mouni

Option of ls:
         (1)-a:To list all hidden files
         Syntax:$ls –a dir name
         Output:
         [maha@localhost ~]$ls –a vandu44
         selvaraj40
         vandu44
         raj34
         (2)-d: To list the current name of the directory.
         Syntax: $ls –d dir name
         Output:
         [maha@localhost ~]$ ls -d vandu44
         Vandu44
         (3)-f:To show the directories with trailing or executable file with trailing
         Syntax: $ls –f dir name
         Output:
         [maha@localhost ~]$ ls –f
         divya
         .dha.1.swp
         honey
         gopal
         (4)-g:To show the group ownership of the file listening
         Syntax:$ ls –g dir name
         Output:
         [maha@localhost ~]$ ls –g
         drwxrwxr-x  2 maha    4096 Dec  8 23:14 student
         drwxrwxr-x  2 maha    4096 Dec  7 01:37 success
         (5)-i:To print the inode number of file
         Syntax: $ls –i dir name
         Output:
         [maha@localhost ~]$ ls –i
         10570991 stat.c
         10570953 stdnt
         10570743 student


         (6)-l:To long list the given details directories and files
         Syntax: $ls –l dir  name
          [maha@localhost ~]$ ls –l
         -rw-rw-r--  1 maha maha     118 Dec 16 01:43 yaac.l
         -rw-rw-r--  1 maha maha     224 Dec 19 00:09 yaac.y

         (7)-r: To list all subdirectories encountered
         Syntax:$ls –r dir name
         Output:
         [maha@localhost ~]$ ls –r
         cdjaya16
         cdjanu15
         cdjanu
         (8)-t:To sort by time
         Syntax:$ls –t dir name
         Output:
         [maha@localhost ~]$ ls –t
         kathir_king
         elakya13
         amkathir
7.cat:To create a file without entering into vi editor
Syntax:$cat>filename
Output:
[maha@localhost ~]$ cat>mec1
hi welcome
^d
[maha@localhost ~]$ cat>mec2
hello how r u
^d

8.cat:To view the content of the file without entering into vi editor
Syntax:$cat filename
Output:
[maha@localhost ~]$ cat mec1
hi welcome you all
[maha@localhost ~]$ cat mec2
hi welcome
[maha@localhost ~]$ cat>mec3
bye…
9.cp:To copy the file from source to destination
Syntax:$cp file1 file2
Output:
[maha@localhost ~]$ cp mec1 mec2
[maha@localhost ~]$ cat mec2
hi welcome you all
10.rm:To remove the file
Syntax:$rm filename
Output:
[maha@localhost ~]$ rm mec3
[maha@localhost ~]$ cat mec3
cat: mec3: No such file or directory
11.mv:To move the file content from one file to another file or directory
Syntax:$mv file1 file2
Output:
[maha@localhost ~]$ mv mec1 mec2
[maha@localhost ~]$ cat mec2
hi welcome you all

12.file:To display the file type
Syntax:$file filename.c
Output:
[maha@localhost ~]$ file mec2
mec2: ASCII text
13.head:To display the first few lines of a file
Syntax:$head filename
Output:
[maha@localhost ~]$ head sys.c
#include<stdio.h>
#include<unistd.h>
14.tail:To display the last few lines of a file
Syntax:$tail filename
Output:
[maha@localhost ~]$ tail sys.c
printf(“\n File content: %c”,buf3);
close(fd);
return(0);
}
15.grep:To search the content of the file
Syntax:$grep “content” filename
Output:
[maha@localhost ~]$ grep “content” sys.c
#include<stdio.h>
#include<unistd.h>


16.wc:To count the number of words in a file and also count the number of lines and characters in a file
Syntax:$wc filename
Output:
[maha@localhost ~]$ wc sys.c
50 67 841 cc
17.more:To scroll the screen downwords using spacebar
Syntax:$more filename
Output:
[maha@localhost ~]$ more myfile.c
When the content is more than the screen size it can be moved using the spacebar.
18.GROUPING COMMANDS:
(i)Semicolon: To Execute one or more commands at a time
Syntax:$Command1;Command2……..;Commandn
Output:
[maha@localhost ~]$ date;cal
Tue Dec 20 01:27:09 IST 2011
   December 2011
Su Mo Tu We Th Fr Sa
                         1  2  3
 4    5    6    7   8   9 10
11  12 13 14 15 16 17
18  19 20 21 22 23 24
25 26 27 28 29 30 31 

(ii)AND(&&):To execute the first command and then next command
Syntax:$Command1&&Command2……..&&Commandn
Output:
[maha@localhost ~]$ date&&cal
Tue Dec 20 01:27:16 IST 2011
   December 2011
Su Mo Tu We Th Fr Sa
                         1  2  3
 4    5   6    7   8    9 10
11  12  13 14 15 16 17
18  19  20  21 22 23 24
25  26  27 28 29 30 31 
(iii)OR(||):If first command is unsuccessful it will execute the next command
Syntax:$Command1||Command2……..||Commandn
Output:
[maha@localhost ~]$ date||cle
Tue Dec 20 01:27:21 IST 2011
PWD: To display the path name to the current directory
[maha@localhost ~]$ pwd
/home/maha


19.mkdir:To make a directory
Syntax:$mkdir dirname

Output:
[maha@localhost ~]$ mkdir mca1
20.cd:To change the  working directory
Syntax:$cd dirname
Output:
[maha@localhost ~]$ cd mca
21.rmdir:To remove the directory
Syntax:$rmdir dirname
Output:
[maha@localhost mca2]$ls
mca mca1
[maha@localhost  mca]$ rmdir mca
22.pwd:To display the pathname to the current directory
Syntax:$pwd
Output:
[maha@localhost ~]$ pwd
/home/maha

23.sort:To sort the content in the ascending alphabetical order
Syntax:$sort filename
Output:
[maha@localhost mca]$ sort mca1
maha
raja
college
^d








RESULT:
            Thus the program to execute the functions of basic commands in unix has been executed successfully.

ExNo: 2
Date:     
                                                SYSTEM CALLS
AIM:
       To write unix program for system calls fstat(),stat(),lseek() function.

ALGORITHM:
Step 1:   Start the process.
Step 2:   Declare the variables.
Step 3:   Using open function create a file and assign it to it.
Step 4:   Check the variable fd,if is less than 0 then display open error.
Step 5:   Write the content into buffer,obtain the information about the file by
               using  the fstat().
Step 6:   Check the type  of the file and display the file type,userid,
               allocated block and block size.
Step 7:   Return the structure of information about named file using stat().
Step 8:   Check the type of file and display the file types userid allocated blockl and
               Block size.
Step 9:   Using lseek function,set the file offset to its current value plus the offset.
Step 10:          Write the content into another file.
Step 11:          Using lseek function set the byte from the beginning the file.
Step 12:          Display the file content.
Step 13:          Stop the program.




/* SYSTEM CALLS*/
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
char buf1[]="abcdefghij";
char buf2[]="ABCDEFGHIJ";
char buf3[40];
struct stat buf;
char *ptr,*ptr1;
int main()
{
int fd,n;
fd=open("exams3.data",O_RDWR|O_CREAT|O_TRUNC|S_IRUSR|S_IWUSR);
if(fd<0)
printf("Open Error");
if((write(fd,buf1,10))!=10)
printf("buf1 with error");
if(fstat(fd,&buf)<0)
printf("fstat error");
if(S_IS            REG(buf.st_mode))
ptr="REGULAR";
else if(S_ISDIR(buf.st_mode))
ptr="DIRECTORY";
else if(S_ISCHR(buf.st_mode))
ptr="CHARACTER";
else if(S_ISBLK(buf.st_mode))
ptr="BLOCK";
else if(S_ISFIFO(buf.st_mode))
ptr="FIFO";
printf("            FILE TYPE:%s\n",ptr);
printf("USER ID:%d\n",buf.st_uid);
printf("ALLOCATED BLOCK:%d\n",buf.st_blocks);
printf("BLOCKED SIZE:%d\n",buf.st_blksize);
if(stat("Exams3.data",&buf)>0)
printf("stat error\n");
if(S_ISREG(buf.st_mode))
ptr1="REGULAR";
else if(S_ISDIR(buf.st_mode))
ptr1="DIRECTORY";
else if(S_ISCHR(buf.st_mode))
ptr1="CHARACTER";
else if(S_ISBLK(buf.st_mode))
ptr1="BLOCK";
else if(S_ISFIFO(buf.st_mode))
ptr1="FIFO";
printf("FILE TYPE:%s\n",ptr1);
printf("USER ID:%d\n",buf.st_uid);
printf("ALLOCATED BLOCK:%d\n",buf.st_blocks);
printf("BLOCKED SIZE:%d\n",buf.st_blksize);
if((n=lseek(fd,20,SEEK_CUR))<0)
printf("lseek error");
if((write(fd,buf2,10))<0)
printf("buf2 with error");
if(lseek(fd,0,SEEK_SET)<0)
printf("lseek error");
if(n=read(fd,buf3,40)<0)
printf("            read error:");
printf("n:%d",n);
printf("FILE CONTENT:%s",buf3);
close(fd);
return(0);
}










OUTPUT:
[maha@localhost prabha]$ cc stat.c
[maha@localhost prabha]$ ./a.out
FILE TYPE:REGULAR
USER ID:698
ALLOCATED BLOCK:8
BLOCKED SIZE:4096
FILE TYPE:REGULAR
USER ID:698
ALLOCATED BLOCK:8
BLOCKED SIZE:4096
n:40FILE CONTENT:abcdefghij[maha@localhost prabha]$ od -c exams3.data
0000000   a   b   c   d   e   f   g   h   i   j  \0  \0  \0  \0  \0  \0
0000020  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0   A   B
0000040   C   D   E   F   G   H   I   J
0000050
[maha@localhost prabha]$




RESULT:
               Thus the program for system calls fstat(),stat(),lseek() function has been executed successfully.


Ex No: 3
Date  :                                           
IPC USING PIPES
AIM:
            To write UNIX program for performing the Inter Process Communication using pipes.

ALGORITHM:

STEP 1: Start the process.
STEP 2: Declare the variables.
STEP 3: Open a file in read and write mode.
STEP 4: Store the command file data in the variable.
STEP 5: Write the data in file.
STEP 6: Stop the process.













\

/*IPC USING PIPES*/

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
void client(int,int);
void server(int,int);
int main()
{
        int pipe1[2],pipe2[2],cpid;
        pipe(pipe1);
        pipe(pipe2);
        cpid=fork();
        if(cpid>0)
        {
              close(pipe2[1]);
              close(pipe1[0]);
              client(pipe2[0],pipe1[1]);
              waitpid(cpid,NULL,0);
              close(pipe1[0]);
              close(pipe2[1]);
        }
        else if(cpid==0)
       {
              close(pipe1[1];
              close(pipe2[0]);
              server(pipe1[0],pipe2[1]);
              close(pipe2[0]);
           
              close(pipe1[1]);
              exit(0);
        }
}
void client(int readfd,int writefd)
{
        int m,n;
        char buf[500],b[500];
        n=read(STDIN_FILENO,buf,sizeof(buf));
        write(writefd,buf,n);
        m=read(readfd,b,sizeof(b));
        write(1,b,m);
}
void server(int readfd,int writefd)
{
        int fd,m,n;
        char buf[500],c;
        n=read(readfd,buf,sizeof(buf));
        fd=open("c",O_CREAT|O_RDWR);
        m=write(fd,buf,n);
        if(m<0)
        printf("Server with error");
}










OUTPUT:
[mca00@Maharaja mca]$ cc pipes.c
[mca00@Maharaja mca]$ ./a.out
Hai students…..
[mca00@Maharaja mca]$ cat c
Hai students…..



















RESULT:
            The UNIX program for perform the Inter Process Communication has been compiled and successfully executed.


Ex No : 4
Date   :                              
IPC USING MESSAGE QUEUES

AIM:
              To write a UNIX program for performing the Inter Process Communication  using message queues.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Using msgget() function, create a message queues.
Step 4: The message queue id is created by using the msgget() function.
Step 5: Check the value of id, if its equal to -1,print the value.
Step 6: Get the string in one variable and assign to another variable using strlen()
              function.
Step 7: Store the messages in the queue using msgsnd() function.
Step 8: Reverse the string.
Step 9: Print the reversed string and length of the string.
Step 10: Stop the process.














/*IPC USING MESSAGE QUEUES*/

#include<sys/msg.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
int gid,len;
int i,n;
char c[20];
struct
{
long mtype;
char mtext[50];
}
message,buff;
gid=msgget((key_t)11,IPC_CREAT|0666);
if(gid==-1)
{
perror("failure");
exit(1);
}
printf("Enter the string:");
scanf("%s",&c);
n=strlen(c);
strncpy(message.mtext,c,50);
message.mtype=1;

len=strlen(message.mtext);
if(msgsnd(gid,&message,len,0)==-1)
{
perror("failure");
exit(1);
}
printf("Reverse the string:");
for(i=n;i>=0;i--)
{
printf("%c",message.mtext[i]);
}
printf("\nstring length:%d\n",n);
return(0);
}

















OUTPUT:
 [mca32@Maharaja ~]$ vi ipc4.c
 [mca32@Maharaja ~]$ cc ipc4.c
 [mca32@Maharaja ~]$ ./a.out
 Enter the string:charunethra
 Reverse the string:arhtenurahc
 string length:11
                           



















RESULT:
                  The program perform the Inter Process Communication using message queues has been compiled and executed successfully.


Ex. No: 5
Date    :
IPC USING SHARED MEMORY
AIM:
            To write UNIX program for performing the Inter Process Communication using the shared memory.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Using SLmget() function is used to create a segment.
Step 4: Using SLmget() function is used to attach the address space.
Step 5: Print the address which attached using shmid() function.
Step 6: Increment the variable and print the variables.
Step 7: stop the process.
























/*IPC USING SHARED MEMORY*/

#include<stdio.h>
#include<stdlib.h>
#include<sys/msg.h>
#include<sys/ipc.h>
#include<sys/types.h>
#define SHMKEY 75
#define k 1024
int shmid;
main()
{
  int i,*pint;
  char *addr1,*addr2;
  extern cleanup();
  for(i=0;i<20;i++)
     signal(i,cleanup);
  shmid=shmget(SHMKEY,128 *k,0777|IPC_CREAT);
  addr1=shmat(shmid,0,0);
  addr2=shmat(shmid,0,0);
  printf("\n ADDR1 0x%x  ADDR2 0x0x%x\n",addr1,addr2);
  pint=(int*)addr1;
  for(i=0;i<10;i++)
     *pint++=i;
        pint=(int*)addr1;
     *pint=10;
        pint=(int*)addr2;
  for(i=0;i<10;i++)
     printf("\n Index %d %t value%d\t\n",pint++);
     printf("\n");
}
cleanup()
{
    shmctl(shmid,IPC_RMID,0);
    exit(0);
}







































OUTPUT:

 [mca39@Maharaja ss]$ cc shmem.c
 [mca39@Maharaja ss]$ ./a.out

 ADDR1 0xb7f90000  ADDR2 0x0xb7f70000

 Index -1208549376 % value-1208549376

 Index -1208549372 % value-1208549376

 Index -1208549368 % value-1208549376

 Index -1208549364 % value-1208549376

 Index -1208549360 % value-1208549376

 Index -1208549356 % value-1208549376

 Index -1208549352 % value-1208549376

 Index -1208549348 % value-1208549376

 Index -1208549344 % value-1208549376

 Index -1208549340 % value-1208549376

[mca39@Maharaja ss]$








RESULT:
            The UNIX program for performing IPC using shared memory has been compiled and executed successfully.
Ex.No : 6
Date   :
SEMAPHORE SYNCHRONIZATION
AIM:
          To write a UNIX program for performing synchronization using semaphore.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the necessary variables.
Step 3: Using segment function, create a semaphore set.
Step 4: Assign the value for the variables.
Step 5: Using SETALL function, set the values to val variables.
Step 6: Using GETALL function, return the values to val.
Step 7: Display the values of val.
Step 8: Stop the process.





















/*SEMAPHORE SYNCHRONIZATION*/


#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#include<stdio.h>
main()
{
 int semid,i;
 static ushort val[5],val1[5];
 semid=semget(0*20,5,IPC_CREAT|0666);
 printf("\nEnter the values:");
 for( i=0;i<5;i++)
 scanf("%d",&val[i]);
 semctl(semid,0,SETALL,val);
 semctl(semid,0,GETALL,val1);
 printf("\nval1[0]:%d\n val1[1]:%d\n val1[2]:%d\n val1[3]:%d\n     val1[4]:%d\n",val1[0],val1[1],val1[2],val1[3],val1[4]);
}

















OUTPUT:

Enter the values:2 6 4 8 3

 val1[0]:2
 val1[1]:6
 val1[2]:4
 val1[3]:8
 val1[4]:3


























RESULT:
            The UNIX program synchronization using semaphore has been completed successfully.
Ex. No: 7
Date   :
FORK CREATION
AIM:
            To write a UNIX program for creating fork.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the necessary variables.
Step 3: Compare the process id then the value will be displayed.
Step 4: If the pid equals to zero, then increment the glob, var.
Step 5: Print the value for pid, var and glob.
Step 6: Save the process.
Step 7: stop the process.

















/*FORK CREATION*/

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
int glob=6;
int main(void)
{
 int var=88;
 pid_t pid;
 printf("\n Before vfork:");
 if((pid=vfork())<0)
 {
  printf("vfork error");
 }
 else if(pid==0)
 {
  glob++;
  var++;
  _exit(0);
 }
 else
  sleep(2);
  printf("pid=%d,glob=%d,var=%d\n",getpid(),glob,var);
  exit(0);
}




OUTPUT:

[mca39@Maharaja ss]$ cc fork1.c
[mca39@Maharaja ss]$ ./a.out

 Before vfork:pid=7606,glob=7,var=89.























RESULT:
            The UNIX program for fork is created successfully.
Ex.No :8
Date:
SIGNAL HANDLING

AIM:
            To write a UNIX program to handle the signal from the process.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Using fork function get process id.
Step 4: Check if process id is zero, then kill the parent process.
Step 5: Check if process id is non-zero, then call the signals function and display
              the process id.
Step 6: Stop the process.
























/*SIGNAL HANDLING*/

#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
void abc();
int pid,i;
main()
{
    pid=fork();
    if(pid==0)
    {
        sleep(1);
        kill(getppid(),SIGUSR1);
        exit(0);
    }
    else
    {
        signal(SIGUSR1,abc);
        for(i=0;i<100;i++)
        printf("%d:\t",i);
        printf("\n Parent exiting\n");
    }
}
void abc()
{
    printf("\n 1 is:%d",i);
}



OUTPUT:

[mca39@Maharaja ss]$ cc sh.c

[mca39@Maharaja ss]$ ./a.out

0:      1:      2:      3:      4:      5:      6:      7:      8:      9:
10:     11:     12:     13:     14:     15:     16:     17:     18:     19:
20:     21:     22:     23:     24:     25:     26:     27:     28:     29:
30:     31:     32:     33:     34:     35:     36:     37:     38:     39:
40:     41:     42:     43:     44:     45:     46:     47:     48:     49:
50:     51:     52:     53:     54:     55:     56:     57:     58:     59:
60:     61:     62:     63:     64:     65:     66:     67:     68:     69:
70:     71:     72:     73:     74:     75:     76:     77:     78:     79:
80:     81:     82:     83:     84:     85:     86:     87:     88:     89:
90:     91:     92:     93:     94:     95:     96:     97:     98:     99:

 Parent exiting.

[mca39@Maharaja ss]$












RESULT:
            The UNIX program to handle the signal process has executed successfully.
Ex.No: 9
Date:
DOWNLOADING WEBPAGE
AIM:
            To create a UNIX program for downloading WebPages.

ALGORITHM:
Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Define the socket address in structure variables.
Step 4: Define the web address and declare the variables.
Step 5: Use the socket function to create the function.
Step 6: Read the home page and send out the message.
Step 7: Stop the process.

















/*DOWNLOADING WEBPAGE*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/types.h>
#define MAXLINE 4096
#define SERV_PORT 80
#define SA struct sockaddr
#define GET_CMD "GET/HTTP/1.0\r\n\r\n"
int main(int argc,char **argv)
{
            int n,sockfd,ct;
 struct hostent *h;
 struct sockaddr_in servaddr;
 char line[MAXLINE];
 if(argc!=2)
 {
                          perror("Usage: web<Web Address>");
            }
 if((h=gethostbyname(argv[1]))==NULL)
            {
  perror("GethostByName");
 }
           
if((h=gethostbyname(argv[1]))==NULL)
            {
                        perror("GethostByName");
 }
            sockfd=socket(AF_INET,SOCK_STREAM,0);
 if(sockfd<0)
         perror("socket error");
            bzero(&servaddr,sizeof(servaddr));
            servaddr.sin_family=AF_INET;
            servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,inet_ntoa(*((struct in_addr*)
h->_addr)),&servaddr.sin_addr);
`           ct=connect(sockfd,(SA *)&servaddr,sizeof(servaddr));
            if(ct<0)
       perror("Connect Error");
n=snprintf(line,sizeof(line),GET_CMD);
write(sockfd,line,strlen(line));
for(;;)
           {
   if((n=read(sockfd,line,MAXLINE))==0)
   break;
   printf("Reading %d bytes of home page %s \n",n,argv[1]);
   line[n]=0;
   if(fputs(line,stdout)==EOF)
    perror("fputs error");
}
 printf("END_of_Line of home page\n");
 close(sockfd);
}


OUTPUT:

 [mca39@Maharaja ss]$ cc dlweb.c
[mca39@Maharaja ss]$ ./a.out www.yahoo.com

Reading 217 bytes of home page www.yahoo.com
<HEAD>
<TITLE>Invalid HTTP Request</TITLE>
</HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica, Arial">
<B>  Bad request syntax</B>
</FONT>

<!-- default "Invalid HTTP Request" response (400) -->
</BODY>
END_of_Line of home page












RESULT:
            The UNIX program downloading webpage has been executed successfully.
Ex.No: 10
Date:
PACKET CAPTURING SNIFFER
AIM:
            To write a UNIX program to capture the TCP/IP packets using sniffer.

ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables.
Step 3: Enter the choice values.
Step 4: If choice is 1, then sniff TCP Packets.
Step 5: If choice is 2, then sniff UDP datagram.
Step 6: If choice is 3, exit the program.
Step 7: Exit the process.

















/* PACKET CAPTURING SNIFFER */

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/ip.h>
#include<netinet/tcp.h>
#include<stdio.h>
int main()
{
 int fd;
 struct iphdr *ip;
 struct tcphdr *tcp;
 char *t;
 char buffer[8192];
for( i=0;i<81;i++)
        buffer[i]='a';
 int choice,proto;
 printf("\n\n SNIFFER");
 printf("\n ~~~~~~~");
 printf( "\nMENU\n");
 printf("\n1->Sniff TCP Packets");
 printf("\n2->Sniff UDP Datagram");
 printf("\n3->Exit\n");
 printf("\nEnter the choice:");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("Start sniffing TCP Packages \n Press ctrl+C stop\n\n");
        proto=IPPROTO_TCP;
        break;
  case 2:
        printf("Start sniffing UDP \n Press ctrl+c stop\n\n");
        proto=IPPROTO_UDP;
        break;
  case 3:
        return 0;
        main();
        return 0;
 }
            fd=socket(PF_INET,SOCK_RAW,proto);
 while(read(fd,buffer,8192)>0)
 {
  ip=(struct iphdr *)buffer;
  tcp=(struct tcphdr *)(buffer+sizeof(struct iphdr));
  t=(char *)&ip->saddr;
  if(proto==IPPROTO_TCP)
        printf("\n PACKET RECEIVED!\n");
  else
       printf("\n DATAGRAM RECEIVED!\n");
       printf("\n Source ip:%d.%d.%d.%d",(t[0]+256)%256,
                  (t[1]+256)%256,(t[2]+256)%256,(t[3]+256)%256);
  t==(char *)&ip->daddr;
  printf("\n Destination ip:%d.%d.%d.%d",(t[0]+256)%256,
               (t[1]+256)%256,(t[2]+256)%256,(t[3]+256)%256);
  printf("\n Source port:%d \n Destination port:%d",tcp->source,tcp>dest);
  printf("\n Data: %s\n",buffer+sizeof(struct iphdr)+sizeof(struct tcphdr));
  printf("\n_______________\n");
 }
return 0;
}

OUTPUT:

[mca39@Maharaja ss]$ cc sniffer.c
[mca39@Maharaja ss]$ ./a.out

SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:1
Start sniffing TCP Packages
 Press ctrl+C stop



 PACKET RECEIVED!

 Source ip: 97.97.97.97
 Destination ip: 97.97.97.97
 Source port: 24929
 Destination port: 24929
 Data: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaQ


















SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:2
Start sniffing UDP
 Press ctrl+c stop



 DATAGRAM RECEIVED!

 Source ip:97.97.97.97
 Destination ip:97.97.97.97
 Source port:24929
 Destination port:24929
 Data: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaQ







SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:3
[mca39@Maharaja ss]$





RESULT:
            The UNIX program has been compiled and executed successfully.
Ex. No: 11
Date   :                          
                                           UDP SOCKETS

AIM:
           To write a UNIX program for perform the UDP connection for the communication.

ALGORITHM:
Step 1: Start the process.
Step 2: Define port and host address, declare the necessary variables for the client.
Step 3: Define the socket address in structure variable servadd and initialize it.
Step 4: Use the socket function create and request to the server.
Step 6: Define the port address function to create and build using bind function.
Step 7: Use the socket function create and request the message.
Step 8: Stop the process.

















/* UDP SOCKETS */
UDP CLIENT:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
main()
{
             int d,r,q,b;
 char c[100];
 struct sockaddr_in cli;
             d=socket(AF_INET,SOCK_DGRAM,0);
 bzero((char *)&cli,sizeof(cli));
 if(d<0)
                        printf("\n socket not created");
             cli.sin_family=AF_INET;
 cli.sin_port=htons(9221);
 cli.sin_addr.s_addr=INADDR_ANY;
 printf("connected to the socket %x\n\n",htons(5555));
 do
 {
  printf("\nEnter the msg to server");
                          fgets(c,100,stdin);
                          sendto(d,&c,sizeof(c),0,(struct sockaddr *)&cli,sizeof(struct sockaddr));                   
                           printf("From server");
                         

 recvfrom(d,&c,sizeof(c),0,(struct sockaddr *)&cli,&r);
                           fputs(c,stdout);
           }
            while(strncmp(c,"exit",4)!=0);
            shutdown(d,1);
}



























UDP SERVER:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
main()
{
            int d,r,q,b;
            char c[100];
            struct sockaddr_in ser,cli;
d=socket(AF_INET,SOCK_DGRAM,0);
            bzero((char*)&ser,sizeof(ser));
            if(d<0)
                         printf("\n socket not created");
            ser.sin_family=AF_INET;
            ser.sin_port=htons(4000);
            ser.sin_addr.s_addr=INADDR_ANY;
            r=sizeof(struct sockaddr);
            b=bind(d,(struct sockaddr*)&ser,r);
            if(b<0)
            {
                        printf("socket not binded properly");
                        return 0;
            }
            printf("\n connect to the socket%x\n\n",htons(5555));
            do
            {
                        recvfrom(d,&c,sizeof(c),0,(struct sockaddr*)&ser,&r);
                        printf("\n THE MESSAGE FROM THE CLIENT IS:");
                        fputs(c,stdout);
                        printf("to server");
                        fgets(c,100,stdin);
                        sendto(d,&c,sizeof(c),0,(struct sockaddr*)&ser,sizeof(struct sockaddr));
            }
            while(strncmp(c,"exit",4)!=0);
            shutdown(q,1);
   }






















OUTPUT

Server Output:
[mca39@Maharaja ss]$ cc udps.c
[mca39@Maharaja ss]$ ./a.out

 connect to the socketb315

 THE MESSAGE  FROM THE CLIENT IS: hai........
To client:  welcome....

Client Output:

[mca39@Maharaja ss]$ cc udpc.c
[mca39@Maharaja ss]$ ./a.out

connected to the socket b315

Enter the msg to server
hai........
From server: welcome....






RESULT:
            The UNIX program has performed UDP connection for the communication has been compiled and executed successfully.
Ex. No: 12
Date:
TCP/IP SOCKETS (Client & Server)
AIM:
            To write a UNIX program that performs the TCP connection for the communication.

ALGORITHM:
Step 1: Start the program.
Step 2: Define port and host address, declare the variables for the client.
Step 3: Define the socket address in structure variables servadd and initialize it.
Step 4: Use the socket function create and request to the server.
Step 5: Define the port address function to create and bind function.
Step 6: Use the socket function create and request to the message.
Step 7: Wait and accept the request then receive the message.
Step 8: Stop the program.















/*TCP/IP SOCKETS*/
TCP CLIENT:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
#define SERV_TCP_PORT 2100
#define SERV_HOST_ADDR "192.168.0.254"
main()
{
        char buff[5];
        int sd;
        struct sockaddr_in servadd;
        bzero((char *)&servadd,sizeof(servadd));
        servadd.sin_family=AF_INET;
        servadd.sin_addr.s_addr=INADDR_ANY;
        servadd.sin_port=8090;
        sd=socket(AF_INET,SOCK_STREAM,0);
        if(sd<0)
                printf("Socket Error");
        if(connect(sd,(struct sockaddr *)&servadd,sizeof(servadd))<0)
                printf("\nConnection Failed");
                printf("\nEnter the message : ");
                scanf("%s",&buff);
                printf("Given message : %s\n",buff);
                send(sd,&buff,50,0);
                printf("\n %s",buff);
        close(sd);
}
TCP SERVER:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
main()
{
        char buff[50];
        int sd,newsd,fd,strlen;
        struct sockaddr_in servadd,cliadd;
        bzero((char *)&servadd,sizeof(servadd));
        servadd.sin_family=AF_INET;
        servadd.sin_addr.s_addr=INADDR_ANY;
        servadd.sin_port=8090;
        sd=socket(AF_INET,SOCK_STREAM,0);
        if(sd>0)
                printf("\nSocket Error");
                printf("\n sd is : %d\n",sd);
            if((bind(sd,(struct sockaddr_in *)&servadd,sizeof(servadd)))>0)
                printf("\nBinding Error\n");
                listen(sd,1);
                strlen=sizeof(cliadd);
                newsd=accept(sd,(struct sockaddr_in *)&cliadd,&strlen);
                printf("New sd : %d\n",newsd);
                recv(newsd,&buff,50,0);
                printf("\nReceived Message : %s\n",buff);
        close(sd);
}

OUTPUT:

TCP Client output:
[mca7@Maharaja bhamila07]$ cc tcpc.c
 [mca7@Maharaja bhamila07]$ ./a.out
Enter the message : hai
Given message : hai
 [mca7@Maharaja bhamila07]$

TCP Server output:

[mca7@Maharaja bhamila07]$ cc tcps.c
tcps.c: In function âmainâ:
tcps.c:12: warning: incompatible implicit declaration of built-in function âbzeroâ
tcps.c:20: warning: passing argument 2 of âbindâ from incompatible pointer type
tcps.c:24: warning: passing argument 2 of âacceptâ from incompatible pointer type
[mca7@Maharaja bhamila07]$ ./a.out

Socket Error
 sd is : 3
New sd : 4

Received Message : hai
[mca7@Maharaja bhamila07]$





RESULT:
            The TCP communication for client and server is executed successfully.
Ex.No :8
Date:
SIGNAL HANDLING
AIM:
            To write a UNIX program to handle the signal from the process.

ALGORITHM:

Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Using fork function get process id.
Step 4: Check if process id is zero, then kill the parent process.
Step 5: Check if process id is non-zero, then call the signals function and display
              the process id.
Step 6: Stop the process.
























/*SIGNAL HANDLING*/

#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
void abc();
int pid,i;
main()
{
    pid=fork();
    if(pid==0)
    {
        sleep(1);
        kill(getppid(),SIGUSR1);
        exit(0);
    }
    else
    {
        signal(SIGUSR1,abc);
        for(i=0;i<100;i++)
        printf("%d:\t",i);
        printf("\n Parent exiting\n");
    }
}
void abc()
{
    printf("\n 1 is:%d",i);
}



OUTPUT:

[mca39@Maharaja ss]$ cc sh.c

[mca39@Maharaja ss]$ ./a.out

0:      1:      2:      3:      4:      5:      6:      7:      8:      9:
10:     11:     12:     13:     14:     15:     16:     17:     18:     19:
20:     21:     22:     23:     24:     25:     26:     27:     28:     29:
30:     31:     32:     33:     34:     35:     36:     37:     38:     39:
40:     41:     42:     43:     44:     45:     46:     47:     48:     49:
50:     51:     52:     53:     54:     55:     56:     57:     58:     59:
60:     61:     62:     63:     64:     65:     66:     67:     68:     69:
70:     71:     72:     73:     74:     75:     76:     77:     78:     79:
80:     81:     82:     83:     84:     85:     86:     87:     88:     89:
90:     91:     92:     93:     94:     95:     96:     97:     98:     99:

 Parent exiting.

[mca39@Maharaja ss]$











RESULT:
            The UNIX program to handle the signal process has executed successfully.

Ex.No: 9
Date:
DOWNLOADING WEBPAGE

AIM:
            To create a UNIX program for downloading WebPages.

ALGORITHM:

Step 1: Start the process.
Step 2: Declare the variables.
Step 3: Define the socket address in structure variables.
Step 4: Define the web address and declare the variables.
Step 5: Use the socket function to create the function.
Step 6: Read the home page and send out the message.
Step 7: Stop the process.















/*DOWNLOADING WEBPAGE*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<strings.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<errno.h>
#include<netinet/in.h>
#include<sys/types.h>
#define MAXLINE 4096
#define SERV_PORT 80
#define SA struct sockaddr
#define GET_CMD "GET/HTTP/1.0\r\n\r\n"
int main(int argc,char **argv)
{
            int n,sockfd,ct;
 struct hostent *h;
 struct sockaddr_in servaddr;
 char line[MAXLINE];
 if(argc!=2)
 {
                          perror("Usage: web<Web Address>");
            }
 if((h=gethostbyname(argv[1]))==NULL)
            {
  perror("GethostByName");
 }
           
if((h=gethostbyname(argv[1]))==NULL)
            {
                        perror("GethostByName");
 }
            sockfd=socket(AF_INET,SOCK_STREAM,0);
 if(sockfd<0)
         perror("socket error");
            bzero(&servaddr,sizeof(servaddr));
            servaddr.sin_family=AF_INET;
            servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,inet_ntoa(*((struct in_addr*)
h->_addr)),&servaddr.sin_addr);
`           ct=connect(sockfd,(SA *)&servaddr,sizeof(servaddr));
            if(ct<0)
       perror("Connect Error");
n=snprintf(line,sizeof(line),GET_CMD);
write(sockfd,line,strlen(line));
for(;;)
           {
   if((n=read(sockfd,line,MAXLINE))==0)
   break;
   printf("Reading %d bytes of home page %s \n",n,argv[1]);
   line[n]=0;
   if(fputs(line,stdout)==EOF)
    perror("fputs error");
}
 printf("END_of_Line of home page\n");
 close(sockfd);
}


OUTPUT:

 [mca39@Maharaja ss]$ cc dlweb.c
[mca39@Maharaja ss]$ ./a.out www.yahoo.com

Reading 217 bytes of home page www.yahoo.com
<HEAD>
<TITLE>Invalid HTTP Request</TITLE>
</HEAD>
<BODY BGCOLOR="white" FGCOLOR="black">
<FONT FACE="Helvetica, Arial">
<B>  Bad request syntax</B>
</FONT>

<!-- default "Invalid HTTP Request" response (400) -->
</BODY>
END_of_Line of home page









RESULT:
            The UNIX program downloading webpage has been executed successfully.



Ex.No: 10
Date:
PACKET CAPTURING SNIFFER
AIM:
            To write a UNIX program to capture the TCP/IP packets using sniffer.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the variables.
Step 3: Enter the choice values.
Step 4: If choice is 1, then sniff TCP Packets.
Step 5: If choice is 2, then sniff UDP datagram.
Step 6: If choice is 3, exit the program.
Step 7: Exit the process.


















/* PACKET CAPTURING SNIFFER */

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/ip.h>
#include<netinet/tcp.h>
#include<stdio.h>
int main()
{
 int fd;
 struct iphdr *ip;
 struct tcphdr *tcp;
 char *t;
 char buffer[8192];
for( i=0;i<81;i++)
        buffer[i]='a';
 int choice,proto;
 printf("\n\n SNIFFER");
 printf("\n ~~~~~~~");
 printf( "\nMENU\n");
 printf("\n1->Sniff TCP Packets");
 printf("\n2->Sniff UDP Datagram");
 printf("\n3->Exit\n");
 printf("\nEnter the choice:");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
        printf("Start sniffing TCP Packages \n Press ctrl+C stop\n\n");
        proto=IPPROTO_TCP;
        break;
  case 2:
        printf("Start sniffing UDP \n Press ctrl+c stop\n\n");
        proto=IPPROTO_UDP;
        break;
  case 3:
        return 0;
        main();
        return 0;
 }
            fd=socket(PF_INET,SOCK_RAW,proto);
 while(read(fd,buffer,8192)>0)
 {
  ip=(struct iphdr *)buffer;
  tcp=(struct tcphdr *)(buffer+sizeof(struct iphdr));
  t=(char *)&ip->saddr;
  if(proto==IPPROTO_TCP)
        printf("\n PACKET RECEIVED!\n");
  else
       printf("\n DATAGRAM RECEIVED!\n");
       printf("\n Source ip:%d.%d.%d.%d",(t[0]+256)%256,
                  (t[1]+256)%256,(t[2]+256)%256,(t[3]+256)%256);
  t==(char *)&ip->daddr;
  printf("\n Destination ip:%d.%d.%d.%d",(t[0]+256)%256,
               (t[1]+256)%256,(t[2]+256)%256,(t[3]+256)%256);
  printf("\n Source port:%d \n Destination port:%d",tcp->source,tcp>dest);
  printf("\n Data: %s\n",buffer+sizeof(struct iphdr)+sizeof(struct tcphdr));
  printf("\n_______________\n");
 }
return 0;
}

OUTPUT:

[mca39@Maharaja ss]$ cc sniffer.c
[mca39@Maharaja ss]$ ./a.out

SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:1
Start sniffing TCP Packages
 Press ctrl+C stop



 PACKET RECEIVED!

 Source ip: 97.97.97.97
 Destination ip: 97.97.97.97
 Source port: 24929
 Destination port: 24929
 Data: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaQ


















SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:2
Start sniffing UDP
 Press ctrl+c stop



 DATAGRAM RECEIVED!

 Source ip:97.97.97.97
 Destination ip:97.97.97.97
 Source port:24929
 Destination port:24929
 Data: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaQ







SNIFFER
 ~~~~~~~
MAIN

1->Sniff TCP Packets
2->Sniff UDP Datagram
3->Exit

Enter the choice:3
[mca39@Maharaja ss]$




RESULT:
            The UNIX program has been compiled and executed successfully.

Ex. No: 11
Date   :                          
                                           UDP SOCKETS

AIM:
           To write a UNIX program for perform the UDP connection for the communication.

ALGORITHM:

Step 1: Start the process.
Step 2: Define port and host address, declare the necessary variables for the client.
Step 3: Define the socket address in structure variable servadd and initialize it.
Step 4: Use the socket function create and request to the server.
Step 6: Define the port address function to create and build using bind function.
Step 7: Use the socket function create and request the message.
Step 8: Stop the process.
















/* UDP SOCKETS */

UDP CLIENT:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
main()
{
             int d,r,q,b;
 char c[100];
 struct sockaddr_in cli;
             d=socket(AF_INET,SOCK_DGRAM,0);
 bzero((char *)&cli,sizeof(cli));
 if(d<0)
                        printf("\n socket not created");
             cli.sin_family=AF_INET;
 cli.sin_port=htons(9221);
 cli.sin_addr.s_addr=INADDR_ANY;
 printf("connected to the socket %x\n\n",htons(5555));
 do
 {
  printf("\nEnter the msg to server");
                          fgets(c,100,stdin);
                          sendto(d,&c,sizeof(c),0,(struct sockaddr *)&cli,sizeof(struct sockaddr));                  
                           printf("From server");
                         

 recvfrom(d,&c,sizeof(c),0,(struct sockaddr *)&cli,&r);
                           fputs(c,stdout);
           }
            while(strncmp(c,"exit",4)!=0);
            shutdown(d,1);
}

























UDP SERVER:

#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
main()
{
            int d,r,q,b;
            char c[100];
            struct sockaddr_in ser,cli;
d=socket(AF_INET,SOCK_DGRAM,0);
            bzero((char*)&ser,sizeof(ser));
            if(d<0)
                         printf("\n socket not created");
            ser.sin_family=AF_INET;
            ser.sin_port=htons(4000);
            ser.sin_addr.s_addr=INADDR_ANY;
            r=sizeof(struct sockaddr);
            b=bind(d,(struct sockaddr*)&ser,r);
            if(b<0)
            {
                        printf("socket not binded properly");
                        return 0;
            }
            printf("\n connect to the socket%x\n\n",htons(5555));
            do
            {
                        recvfrom(d,&c,sizeof(c),0,(struct sockaddr*)&ser,&r);
                        printf("\n THE MESSAGE FROM THE CLIENT IS:");
                        fputs(c,stdout);
                        printf("to server");
                        fgets(c,100,stdin);
                        sendto(d,&c,sizeof(c),0,(struct sockaddr*)&ser,sizeof(struct sockaddr));
            }
            while(strncmp(c,"exit",4)!=0);
            shutdown(q,1);
   }






















OUTPUT

Server Output:
[mca39@Maharaja ss]$ cc udps.c
[mca39@Maharaja ss]$ ./a.out

 connect to the socketb315

 THE MESSAGE  FROM THE CLIENT IS: hai........
To client:  welcome....

Client Output:

[mca39@Maharaja ss]$ cc udpc.c
[mca39@Maharaja ss]$ ./a.out

connected to the socket b315

Enter the msg to server
hai........
From server: welcome....






RESULT:
            The UNIX program has performed UDP connection for the communication has been compiled and executed successfully.

Ex. No: 12
Date:
TCP/IP SOCKETS (Client & Server)
AIM:
            To write a UNIX program that performs the TCP connection for the communication.

ALGORITHM:
Step 1: Start the program.
Step 2: Define port and host address, declare the variables for the client.
Step 3: Define the socket address in structure variables servadd and initialize it.
Step 4: Use the socket function create and request to the server.
Step 5: Define the port address function to create and bind function.
Step 6: Use the socket function create and request to the message.
Step 7: Wait and accept the request then receive the message.
Step 8: Stop the program.















/*TCP/IP SOCKETS*/
TCP CLIENT:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
#define SERV_TCP_PORT 2100
#define SERV_HOST_ADDR "192.168.0.254"
main()
{
        char buff[5];
        int sd;
        struct sockaddr_in servadd;
        bzero((char *)&servadd,sizeof(servadd));
        servadd.sin_family=AF_INET;
        servadd.sin_addr.s_addr=INADDR_ANY;
        servadd.sin_port=8090;
        sd=socket(AF_INET,SOCK_STREAM,0);
        if(sd<0)
                printf("Socket Error");
        if(connect(sd,(struct sockaddr *)&servadd,sizeof(servadd))<0)
                printf("\nConnection Failed");
                printf("\nEnter the message : ");
                scanf("%s",&buff);
                printf("Given message : %s\n",buff);
                send(sd,&buff,50,0);
                printf("\n %s",buff);
        close(sd);
}
TCP SERVER:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<fcntl.h>
main()
{
        char buff[50];
        int sd,newsd,fd,strlen;
        struct sockaddr_in servadd,cliadd;
        bzero((char *)&servadd,sizeof(servadd));
        servadd.sin_family=AF_INET;
        servadd.sin_addr.s_addr=INADDR_ANY;
        servadd.sin_port=8090;
        sd=socket(AF_INET,SOCK_STREAM,0);
        if(sd>0)
                printf("\nSocket Error");
                printf("\n sd is : %d\n",sd);
            if((bind(sd,(struct sockaddr_in *)&servadd,sizeof(servadd)))>0)
                printf("\nBinding Error\n");
                listen(sd,1);
                strlen=sizeof(cliadd);
                newsd=accept(sd,(struct sockaddr_in *)&cliadd,&strlen);
                printf("New sd : %d\n",newsd);
                recv(newsd,&buff,50,0);
                printf("\nReceived Message : %s\n",buff);
        close(sd);
}

OUTPUT:

TCP Client output:
[mca7@Maharaja bhamila07]$ cc tcpc.c
 [mca7@Maharaja bhamila07]$ ./a.out
Enter the message : hai
Given message : hai
 [mca7@Maharaja bhamila07]$

TCP Server output:

[mca7@Maharaja bhamila07]$ cc tcps.c
tcps.c: In function âmainâ:
tcps.c:12: warning: incompatible implicit declaration of built-in function âbzeroâ
tcps.c:20: warning: passing argument 2 of âbindâ from incompatible pointer type
tcps.c:24: warning: passing argument 2 of âacceptâ from incompatible pointer type
[mca7@Maharaja bhamila07]$ ./a.out

Socket Error
 sd is : 3
New sd : 4

Received Message : hai
[mca7@Maharaja bhamila07]$





RESULT:
            The TCP communication for client and server is executed successfully.

No comments:

Post a Comment

PLEASE ENTER THE VALUABLE COMMENTS