Different Types of Patterns in shell script

Welcome back!!!! Her in this version of our post we are going to see some tricky patterns in linux shell script or bash programming. I hope the code I have made for this pattern program is surly written in easy way for beginner as well as professional. This post included some of the most tricky patterns like star triangle, star pyramid ,inverted pyramid, alphabet triangle,alphabet pyramid,number triangle number pyramid and so on.

Any way let us enjoy with code…………………………

1.A SHELL SCRIPT TO DISPLAY TRIANGULAR PATTERN USING STAR(*)

# A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size"
read n

for((i=1; i<=n; i++))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "* "
  done
  echo 
done

OUT PUT

enter size of your pattern
7
your triangular pattern is:
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 

2. A SHELL SCRIPT TO DISPLAY INVERTED TRIANGULAR PATTERN USING STAR(*)

# A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size"
read n

for((i=n; i>=1; i--)
do 
  for((j=1; j<=i;j++))
  do
    echo -n "* "
  done
  echo 
done

OUT PUT

enter size of your pattern
7
your triangular pattern is:
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

3.A SHELL SCRIPT CODE TO DISPLAY TRIANGULAR NUMBER PATTERN

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your triangular pattern is:"
for((i=1; i<=n; i++))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "$j "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your triangular pattern is:
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 5 6 
1 2 3 4 5 6 7 

4.A SHELL SCRIPT CODE TO DISPLAY INVERTED NUMBER TRIANGLE

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your triangular pattern is:"
for((i=n; i>=1; i--))
do 
  for((j=1; j<=i;j++))
  do
    echo -n "$j "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your triangular pattern is:
1 2 3 4 5 6 7 
1 2 3 4 5 6 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

5.A SHELL SCRIPT CODE TO DISPLAY PASCAL’S TRIANGLE

#A SHELL SCRIPT CODE TO PRINT PASCAL'S TRIANGLE

#!/bin/sh
coff=1
echo "enter size of your triangle"
read n
for((i=0; i<=n; i++))
do
  for((space=1; space<=n-i; space++))
  do
    echo -n " "
  done
  for((j=0; j<=i; j++))
  do
    if [[ $j == 0 || $i == 0 ]]
    then
        coff=1
   else
      coff=$((coff*(i-j+1)/j))
  fi
  echo -n $coff " "
done
echo " "
done
OUT PUT

enter size of your triangle
4
    1   
   1  1   
  1  2  1   
 1  3  3  1   
1  4  6  4  1   

6.A shell script code to display Floyd’s triangle


# shell script to display Floyd's triangle

#!/bin/sh
k=1
echo "enter the size of your pattern"
read row
echo "your Floyd's triangle is:"

for((i=1; i<row; ++i))
do
  for((j=1; j<=i; ++j))
  do
    echo -n "$k "
    ((++k))
  done
  echo
done
OUT PUT

enter the size of your pattern
5
your Floyd's triangle is:
1 
2 3 
4 5 6 
7 8 9 10 

7.A shell script code to display pyramid of pattern using star(*)

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your pyramidal pattern is:"
for((i=1; i<=n; i++))
do 
  for((space=1; space<=n-i; space++))
  do
     echo -n " "
  done
  for((j=1; j<=i; j++))
  do
    echo -n "* "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your pyramidal pattern is:
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

8.A shell script code to display inverted pyramidal pattern using star(*)

 #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE

#!/bin/sh
echo "enter size of your pattern"
read n
echo "your pyramidal pattern is:"
for((i=n; i>=1; i--))
do 
  for((space=1; space<=n-i; space++))
  do
     echo -n " "
  done
  for((j=1; j<=i; j++))
  do
    echo -n "* "
  done
  echo 
done







OUT PUT

enter size of your pattern
7
your pyramidal pattern is:
* * * * * * * 
 * * * * * * 
  * * * * * 
   * * * * 
    * * * 
     * * 
      * 

9.A shell script code to display alphabet triangular pattern


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "alphabet pattern of size $row is:"

for((i=1; i<=row; i++))
do

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F 
A B C D E F G 

10.A shell script code to display inverted alphabet triangular pattern

# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "alphabet pattern of size $row is:"

for((i=row; i>=1; i--)
do

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
A B C D E F G 
A B C D E F 
A B C D E 
A B C D 
A B C 
A B 
A 

11.A shell script code to display alphabet pyramid


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "pattern of size $row is:"
for((i=1; i<=row; i++))
do
   for((space=1; space<=row-i; space++))
   do
      echo -n " "
   done

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
7
pattern of size 7 is:
      A 
     A B 
    A B C 
   A B C D 
  A B C D E 
 A B C D E F 
A B C D E F G 

12.A shell script code to display inverted alphabet of pyramid


# shell scrip to display pyramid of alphabet

#!/bin/sh
alpha=( {A..Z} )
echo "enter the size of your pattern"
read row
echo "pattern of size $row is:"
for((i=row; i>=1; i--))
do
   for((space=1; space<=row-i; space++))
   do
      echo -n " "
   done

   for((j=0; j<i; j++))
   do 
      echo -n "${alpha[j]} "

   done
   echo
done
OUT PUT

enter the size of your pattern
6
pattern of size 6 is:
A B C D E F 
 A B C D E 
  A B C D 
   A B C 
    A B 
     A 

13.A shell script to display binary triangular number pattern

finally I would like to show you one more unique pattern which is of binary number form. we can create any type of binary number pattern but now i will write one code to display mixed binary digit pattern which is little tricky the pattern looks like as the one given below. ya exactly it’s so beautiful let see the code also

1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 0 1 

See the code below it seems difficult but chill it is just easy......
# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The binary number pattern of size $n is:"
for((i=1; i<=n; i++))
 do
   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
         echo -n "1 "
      else
         echo -n "0 "
    fi
   done
   echo
done

OUT PUT
enter size of your pattern
7
The binary number pattern of size 7 is:

1 
0 1 
1 0 1 
0 1 0 1 
1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 0 1 

14.A shell script to display inverted pattern of binary number

Here I am going to show you the inverted binary number pattern in a triangular form which is given above in a normal form

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The binary number pattern of size $n is:"
for((i=n; i>=1; i--)
 do
   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
         echo -n "1 "
      else
         echo -n "0 "
    fi
   done
   echo
done
OUT PUT

enter size of your pattern
7
The inverted binary pattern of size 7 is:
1 0 1 0 1 0 1 
0 1 0 1 0 1 
1 0 1 0 1 
0 1 0 1 
1 0 1 
0 1 
1 

15.A shell script to display pyramidal binary number pattern

Here also I am going to show you how to display the binary number format given above in a pyramidal format

let’s go here is the code.

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The pyramidal binary pattern of size $n is:"
for((i=1; i<=n; i++))
do
   for((space=1; space<=n-i; space++))
   do
      echo -n " "
   done

   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
          echo -n "1 "
      else
         echo -n "0 "
     fi
    done
   echo
done

enter size of your pattern
7
The inverted binary pattern of size 7 is:
      1 
     0 1 
    1 0 1 
   0 1 0 1 
  1 0 1 0 1 
 0 1 0 1 0 1 
1 0 1 0 1 0 1 

16.A shell script to display an inverted pyramidal binary number pattern.

Finally I would like to show you the binary number inverted pyramidal pattern which is the end of this post.

# A SHELL SCRIPT CODE TO DISPLAY PATTERN
# OF BINARY DIGIT 

#!/bin/sh
coff=1
echo "enter size of your pattern"
read n
echo "The pyramidal binary pattern of size $n is:"
for((i=n; i>=1; i--))
do
   for((space=1; space<=n-i; space++))
   do
      echo -n " "
   done

   for((j=1; j<=i; j++))
   do
      if (( (i+j)%2==0 ))
      then
          echo -n "1 "
      else
         echo -n "0 "
     fi
    done
   echo
done

OUT PUT

enter size of your pattern
7
The pyramidal binary pattern of size 7 is:
1 0 1 0 1 0 1 
 0 1 0 1 0 1 
  1 0 1 0 1 
   0 1 0 1 
    1 0 1 
     0 1 
      1 

Thanks for your comment. please don’t forget to share the site to your friends.

6. C program to create a TCP socket in which three clients communicate to each other through a single server.

In our previous post we have seen how two client can communicate to each other through a single server. But now upgrading the same concept we are going to see how three client can communicate to each other . In these post I am going to show you a TCP socket in which two client will send a number to the server and the server will add the two number and forward it to the third client. hopefully after these tutorial you all can extend the program for any number of client to be able to communicate over a single client.

/*SERVER.c*/
/*Here in the server we will receive the two number from client1 and client2. after adding the two number we will send the sum to client3*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>


int main(){
int a,b,c,arr[100],acc1,acc2,acc3,sock,list,value,len,num1,num2,sum;
char line[100],line1[100];
struct sockaddr_in server,client;

sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
perror("socket not found\n");
exit(1);
}

server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;
bzero(&server.sin_zero,8);

b=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(b==-1){
perror("unable to bind\n");
exit(1);
}

list=listen(sock,5);
if(list==-1){
perror("unable to listen to the client\n");
exit(1);
}
while(1){
printf("connection established waiting for the client\n");

//accept1
len=sizeof(struct sockaddr_in);
acc1=accept(sock,(struct sockaddr *)&client,&len);
if(acc1==-1){
perror("acceptance error\n");
exit(1);
}
//accept2
acc2=accept(sock,(struct sockaddr *)&client,&len);
if(acc2==-1){
perror("acceptance error\n");
exit(1);
}
//accept3
acc3=accept(sock,(struct sockaddr *)&client,&len);
if(acc3==-1){
perror("acceptance error\n");
exit(1);
}

printf("passing information between client.............\n");
do{
//accept from client1 
a=recv(acc1,line,strlen(line)+1,0);
num1=atoi(line);
//accept from client2 
a=recv(acc2,line,strlen(line)+1,0);
num2=atoi(line);
//send the sum to 3rd client
sum=num1+num2;
sprintf(line1,"%d\n",sum);
c=send(acc3,line1,strlen(line1),0);
}

while(abs(strcpy(line,"quit")));

close(acc1);
close(acc2);
return 0;

}
}
















/*CLIENT1.c*/
/*These client will send only an integer to the server but nothing to recieve*/

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd==-1){
    perror("socket connection failed\n");
  exit(1);
 } 


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

printf("enter 1st num to be sent\n: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0); 

}
while(strcpy(line,"quit"));


	
close(sockfd);

 return 0;
}
/*CLIENT2.c*/
/*This client also will send an integer to the server nothing to receive*/

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd==-1){
    perror("socket connection failed\n");
  exit(1);
 } 


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

printf("enter 2st num to be sent\n: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0); 

}
while(strcpy(line,"quit"));


	
close(sockfd);

 return 0;
}
/*CLIENT3.c*/
/*This client will receive the sum of the two number sent by clint1 and client2 where the server performs the addition*/

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd==-1){
    perror("socket connection failed\n");
  exit(1);
 } 


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

n=recv(sockfd,line,100,0);
        printf("sum recieved from client1 and client2  through server=%s\n",line);
exit(1);
}
while(strcpy(line,"quit"));


	
close(sockfd);

 return 0;
}

5. C program to create a TCP socket in which multiple client can communicate to each other through a single server

Her under these version of our post we are going to create a socket program in which two client can communicate to each other through a single server . Just it mean that we are creating a socket program for a simple chat like facebook , messenger and other with any coast. lets see the code. don’t forget to run these code on linux operating system. If you are using a window I will recommend you to use window based linux operating system called ubuntu. you can just download the virtual machine and ubuntu separetly and install. It is open source software.

SERVER.c

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


int main(){
int a,b,c,arr[100],acc1,acc2,sock,list,value,len,num1,num2;
char line[100],line1[100];
struct sockaddr_in server,client;

sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
perror("unable to find the socket\n");
exit(1);
}


server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;
bzero(&server.sin_zero,8);

b=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(b==-1){
perror("unable to bind\n");
exit(1);
}

list=listen(sock,5);
if(list==-1){
perror("listening error");
exit(1);
}

while(1){
printf("connection established waiting for the client\n");

//accept1
len=sizeof(struct sockaddr_in);
acc1=accept(sock,(struct sockaddr *)&client,&len);

//accept2
acc2=accept(sock,(struct sockaddr *)&client,&len);


printf("passing information between client.............\n");
do{
//accept from client1 and send to client2

recv(acc1,line,strlen(line)+1,0);
strcpy(line1,line);
send(acc2,line1,strlen(line1)+1,0);

//accept from client2 and send to clientt1

recv(acc2,line,strlen(line)+1,0);
strcpy(line1,line);
send(acc1,line1,strlen(line1)+1,0);
}

while(strcpy(line,"quit"));

close(acc1);
close(acc2);
return 0;

}
}
















CLIENT1.c

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sock,n;
  char line[100],line1[200],sum[200];
  struct sockaddr_in client;

  sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock==-1){
               perror("unable to find the socket\n");
               exit(1);
    }

 
  client.sin_family=AF_INET;
  client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  client.sin_port=htons(6000);

  bzero(&client.sin_zero,8);


  i=connect(sock,(struct sockaddr *)&client,sizeof(client));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

/* sending message to client1(your friend) */
printf(" write message to your friend............\n: ");
scanf("%s",line);
strcpy(line1,line);
send(sock,line1,strlen(line1)+1,0); 

/* Receiving message from client1(your friend */
n=recv(sock,line,strlen(line)+1,0);
        printf("message from your friend =%s\n",line);

}
while(strcpy(line,"quit"));


	
close(sock);

 return 0;
}
CLIENT2.c

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sock,n;
  char line[100],line1[200],sum[200];
  struct sockaddr_in client;

  sock=socket(AF_INET,SOCK_STREAM,0);
  if(sock==-1){
               perror("unable to find the socket\n");
               exit(1);
    }


  client.sin_family=AF_INET;
  client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  client.sin_port=htons(6000);

  bzero(&client.sin_zero,8);


  i=connect(sock,(struct sockaddr *)&client,sizeof(client));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

/* sending message to client2(your friend) */
printf("write message to your friend..........\n: ");
scanf("%s",line);
strcpy(line1,line);
send(sock,line1,strlen(line1)+1,0); 

/* Recieving message from client2(your friend */
recv(sock,line,strlen(line)+1,0);
        printf("Message from your friend=%s\n",line);
}
while(strcpy(line,"quit"));


	
close(sock);

 return 0;
}

don’t forget to comment share and subscribe the site to get the latest update

4. A c.program to create TCP program through which a client send an integer and the server will reverse the number and forward it to the client.

/*A SERVER CODE TO SORT AN ARRAY AND SEND IT TO THE CLIENT*/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>


int main(){
int sock,len,k,lis,acc,sent;
char line[100],line1[100],line2[100];
int num1,digit=0,rev=0,n;
char buf[]="welcome to socket Programming";
unsigned int rec;
struct sockaddr_in server,client;


sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==-1){
perror("Socket\n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;

bzero(&server.sin_zero,8);

len=sizeof(struct sockaddr_in);
k=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(k==-1){
perror("bind\n");
exit(1);
}

lis=listen(sock,5);
if(lis==-1){
perror("listen\n");
exit(1);
}

while(1){

printf("waiting for client connection on port 6000\n");
acc=accept(sock,(struct sockaddr *)&client,&len);
if(acc==-1){
perror("accept\n");
exit(1);
}


printf("Passing Information to the client............................\n");

memset(line,0x0,100);

n=recv(acc,line,100,0);
num1=atoi(line);

while(num1!=0){
rev=rev*10;
rev=rev+(num1%10);
num1=num1/10;
}


sprintf(line1,"%d\n",rev);

send(acc,line1,strlen(line1),0);


close(acc);
return 0;

}
}

4.2 CLIENT CODE FOR THE ABOVE SERVER PROGRAM

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char buf[100];
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
printf("connection Established successfully continue your operation\n");

/* After connection, the client can send or receive messages.*/
do{
printf("enter the number to be reversed: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0); 

n=recv(sockfd,line,100,0);
        printf("Reverse number received from server=%s\n",line);
}

while(strcpy(line,"quite"));
	
close(sockfd);

 return 0;
}

Please leave your comment

share and subscribe our site if you get some thing good.

3. A c.program to create a TCP socket through which a client sends an array and the server sorts an array and forward it to the clients.

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


int main(){
int sock,len,k,lis,acc,sent;
char line[100],line1[100],line2[100];
int num1,digit=0,rev=0,n,a[100],i,j;
char buf[]="welcome to socket Programming";
unsigned int rec;
struct sockaddr_in server,client;


sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==-1){
perror("Socket\n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;

bzero(&server.sin_zero,8);

len=sizeof(struct sockaddr_in);
k=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(k==-1){
perror("bind\n");
exit(1);
}

lis=listen(sock,5);
if(lis==-1){
perror("listen\n");
exit(1);
}

while(1){

printf("waiting for client connection on port 6000\n");
acc=accept(sock,(struct sockaddr *)&client,&len);
if(acc==-1){
perror("accept\n");
exit(1);
}


printf("Passing Information to the client............................\n");

memset(line,0x0,100);

do{
k=recv(acc,&a,5*sizeof(int),0);

for(i=0;i<5;i++){
for(j=i+1;j<5;j++){
if(a[i]>a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

send(acc,&a,5*sizeof(int),0);
}
while(strcpy(line,"quit"));
close(acc);
return 0;

}
}

A client code for the above server

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>

int main()
{
  int i,sockfd,n,k,a[100],b[100];
  char buf[100];
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
printf("connection Established successfully continue your operation\n");

/* After connection, the client can send or receive messages.*/
do{

   printf("enter 5 array element to be sorted:\n ");
   for(i=0;i<5;i++){
            scanf("%d",&a[i]);
}
   send(sockfd,&a,5*sizeof(int),0); 

   k=recv(sockfd,&b,5*sizeof(int),0);
 
   printf("sorted array received from server\n");
   for(i=0;i<5;i++){
             printf("%d\n ",b[i]);
}
} 
while(strcpy(line,"quite"));
	
close(sockfd);

 return 0;
}

A c.program to create a TCP socket through which a client will send two integer to the server and server will add the two integer and forward the sum to the client.

1.2 SERVER CODE (SERVER.c)

/*A server program to add two number sent from client and send back the sum*/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>


int main(){
int sock,len,k,lis,acc,sent;
char line[100],line1[100],line2[100];
int num1,num2,sum,n;
char buf[]="welcome to socket Programming";
unsigned int rec;
struct sockaddr_in server,client;


sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==-1){
perror("Socket connection failed \n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=htons(6000);
server.sin_addr.s_addr=INADDR_ANY;

bzero(&server.sin_zero,8);

len=sizeof(struct sockaddr_in);
k=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(k==-1){
perror("bind error \n");
exit(1);
}

lis=listen(sock,5);
if(lis==-1){
perror("listen\n");
exit(1);
}

while(1){

printf("waiting for client connection on port 6000\n");
acc=accept(sock,(struct sockaddr *)&client,&len);
if(acc==-1){
perror("accept denied \n");
exit(1);
}

do{
printf("Passing Information to the client............................\n");

memset(line,0x0,100);

n=recv(acc,line,100,0);
num1=atoi(line);

n=recv(acc,line,100,0);
num2=atoi(line);

sum=num1+num2;
sprintf(line1,"%d\n",sum);

send(acc,line1,strlen(line1),0);
}

while(abs(strcpy(line,"quit")));



close(acc);
return 0;

}
}

1.2.1 Now it’s time to proceed to client code (CLIENT.c)

/*COMPUTER NETWORK OF THE CLIENT PROCESS*/

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
int main()
{
  int i,sockfd,n;
  char line[100],num2[200],sum[200];
  struct sockaddr_in sa_addr;

  sockfd=socket(AF_INET,SOCK_STREAM,0);
  if(sockfd==-1){
    perror("socket connection failed\n");
  exit(1);
 } 


  sa_addr.sin_family=AF_INET;
  sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
  sa_addr.sin_port=htons(6000);

  bzero(sa_addr.sin_zero,8);


  i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
  if(i==-1){
    perror("unable to find the server\n");
   }
  else{
     printf("connection Established successfully continue your operation\n");
  }
/* After connection, the client can send or receive messages.*/
do{

printf("enter 1st number: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0); 

printf("enter 2nd number: ");
scanf("%s",line);
send(sockfd,line,strlen(line)+1,0);

n=recv(sockfd,line,100,0);
        printf("sum received from server=%s\n",line);

}
while(strcpy(line,"quit"));


	
close(sockfd);

 return 0;
}

please leave your comment if get some thing using useful.

Socket Programming

Here we are going to deal with some basic TCP socket programming using c

Under this version of my post I am going to provide you some important socket codes with explanation hoping that reader has some concept of computer network as prerequisite so that easily understand can the code.

1.A simple TCP program in c in which server and client communicate to each other (server_client_Interaction)

/*SERVER.c*/
/*NETWORK PROGRAMMING WITH SOCKETS FOR SERVER*/

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
int sock, length,i,list;
unsigned int acc;
char line1[100],line2[100];
struct sockaddr_in server,client;

/*calling socket function*/
sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
           perror("unable to find the socket\n");
           exit(1);
}
 /*already defined member in sockaddr_in structure*/
 server.sin_family=AF_INET;
 server.sin_addr.s_addr=INADDR_ANY;
 server.sin_port=htons(6000);

 bzero(&server.sin_zero,8);

/*calling bind function*/
 i=bind(sock,(struct sockaddr *)&server,sizeof(server));
if(i==-1){
           perror("binding error\n");
           exit(1);
}

/*liten function calling ro listen to the client*/
 list=listen(sock,5);
 if(list==-1){
           perror("listeninig error\n");
           exit(1);
}
else{
    printf("Operation success waiting for client to connect on port 6000\n");
}

while(1){
 /*Accept function to grant acceptance*/
 length=sizeof(client);
 acc=accept(sock, (struct sockaddr *) &client,&length);
 if(acc==-1){
            perror("acceptance error\n");
           exit(1);
}
 /*Message to client*/
strcpy(line1,"Message from Server");
strcpy(line2,"Welcome to Socket Programming");
send(acc, line1, 100, 0);
send(acc, line2, 100, 0);

/*Message from client*/
recv(acc, line1, 100, 0);
printf("%s\n", line1);

close(acc);

return 0;

}
}

In the above code we have written the server program with comment line explanation for each and every pre defined function if you have any problem in understanding please put your comment …… and don”t forget to share subscribe the site if you get some thing useful.

1.1 Client code for simple communication for the server code written above.

/*CLIENT.c*/
/*COMPUTER NETWORK OF THE CLIENT PROCESS*/



#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
  int i,sock;
  char line1[100];
  struct sockaddr_in client;
  
/*calling socket function*/
  sock=socket(AF_INET,SOCK_STREAM,0);
if(sock==-1){
           perror("unable to find the socket\n");
           exit(1);
}

/*already defined member in sockaddr_structure*/
  client.sin_family=AF_INET;
  client.sin_addr.s_addr=inet_addr("127.0.0.1");    /*Loop back IP address*/   
  client.sin_port=htons(6000);   /*port number for communication must be same with server port number*/

 bzero(&client.sin_zero,8);

/*connect function calling*/
  i=connect(sock,(struct sockaddr *)&client,sizeof(client));
if(i==-1){
           perror("connection error please try again\n");
           exit(1);
}
else{
    printf("Congra!!! connected to server successfully\n");
}
/* After connection, the client can send or receive messages.*/

/*receiving message from server*/
	recv(sock, line1, 100, 0);
	printf("%s\n", line1);
/*sending message to server*/

	strcpy(line1,"Message from client");
	send(sock, line1, 100, 0);
	
close(sock);
return 0;

 
}

Now after writing the code on linux operating system (ubutu editor)

open two window to run the server and client then compile both program then absolutely you will be satisfied that easily using one system you can do every thing as client and server simultaneously using loop back ip_address (127.0.0.1) as given in the code.

thanks for visiting us

your comment is a building block for our site

A Java Program To display diffrent window background using Frame class

/*By clicking on the button you can set your window background on your desired color*/
/*I recommend you this code if your eclipse doesn't support applet viewer 
else refer to our applet code for window background which is more easer*/

package p1;


import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class Background extends Frame implements ActionListener {
	Button b1,b2,b3,b4;
	String s="Please click the button for new Background",s2;
	Font f1,f2,f3;
Background() {
	setLayout(new FlowLayout());
	f1 = new Font("Arial",Font.BOLD,18);    
    f2 = new Font("Forte",Font.PLAIN,24);    
    f3 = new Font("Elephant",Font.ITALIC,28);
	b1=new Button("Blue");
	b2=new Button("Green");
	b3=new Button("Cyan");
	b4=new Button("Red");
	add(b1);
	add(b2);
	add(b3);
	add(b4);
	b1.addActionListener(this);
	b2.addActionListener(this);
	b3.addActionListener(this);
	b4.addActionListener(this);
}
public void paint(Graphics g) {
	g.setFont(f1);
	g.drawString(s,30,40);
	g.setFont(f3);
	g.drawString(s2,150,250);
}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String k=e.getActionCommand();
		if(k.equals("Blue")) {
			setBackground(Color.blue);
			s2="This is Blue Color Background";
		}
		if(k.equals("Green")) {
			setBackground(Color.green);
			s2="This is Green Color Background";
		}
		if(k.equals("Cyan")) {
			setBackground(Color.cyan);
			s2="This is Cyan Color Background";
		}
		if(k.equals("Red")) {
			setBackground(Color.red);
			s2="This is Red Color Background";
		}
			
		}
	public static void main(String args[]) {
		Background b=new Background();
		b.setSize(new Dimension(200,300));
		b.setVisible(true);
	}
		
		
	}
	


A Java Program to display diffrent windows Background using an applet class

/*By clicking on the button you can change your windows backgraound as your wish*/

package p1;

import java.applet.*;
import java.awt.event.*;
import java.awt.*;

public class SetBackground extends Applet implements ActionListener {
	Button b1,b2,b3,b4;
	String s="Please click the button for new Background",s2;
	Font f1,f2,f3;
public void init() {
	f1 = new Font("Arial",Font.BOLD,18);    
    f2 = new Font("Forte",Font.PLAIN,24);    
    f3 = new Font("Elephant",Font.ITALIC,28);
	b1=new Button("Blue");
	b2=new Button("Green");
	b3=new Button("Cyan");
	b4=new Button("Red");
	add(b1);
	add(b2);
	add(b3);
	add(b4);
	b1.addActionListener(this);
	b2.addActionListener(this);
	b3.addActionListener(this);
	b4.addActionListener(this);
}
public void paint(Graphics g) {
	g.setFont(f1);
	g.drawString(s,30,40);
	g.setFont(f3);
	g.drawString(s2,150,250);
}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String k=e.getActionCommand();
		if(k.equals("Blue")) {
			setBackground(Color.blue);
			s2="This is Blue Color Background";
		}
		if(k.equals("Green")) {
			setBackground(Color.green);
			s2="This is Green Color Background";
		}
		if(k.equals("Cyan")) {
			setBackground(Color.cyan);
			s2="This is Cyan Color Background";
		}
		if(k.equals("Red")) {
			setBackground(Color.red);
			s2="This is Red Color Background";
		}
			
		}
		
		
	}
  • 5. C program to create a TCP socket in which multiple client can communicate to each other through a single server

    08/08/2019 by

    Her under these version of our post we are going to create a socket program in which two client can communicate to each other through a single server . Just it mean that we are creating a socket program for a simple chat like facebook , messenger and other with any coast. lets see the code. don’t forget to run these code on linux operating system. If you are using a window I will recommend you to use window based linux operating system called ubuntu. you can just download the virtual machine and ubuntu separetly and install. It is open source software.

    SERVER.c
    
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    
    int main(){
    int a,b,c,arr[100],acc1,acc2,sock,list,value,len,num1,num2;
    char line[100],line1[100];
    struct sockaddr_in server,client;
    
    sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock==-1){
    perror("unable to find the socket\n");
    exit(1);
    }
    
    
    server.sin_family=AF_INET;
    server.sin_port=htons(6000);
    server.sin_addr.s_addr=INADDR_ANY;
    bzero(&server.sin_zero,8);
    
    b=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(b==-1){
    perror("unable to bind\n");
    exit(1);
    }
    
    list=listen(sock,5);
    if(list==-1){
    perror("listening error");
    exit(1);
    }
    
    while(1){
    printf("connection established waiting for the client\n");
    
    //accept1
    len=sizeof(struct sockaddr_in);
    acc1=accept(sock,(struct sockaddr *)&client,&len);
    
    //accept2
    acc2=accept(sock,(struct sockaddr *)&client,&len);
    
    
    printf("passing information between client.............\n");
    do{
    //accept from client1 and send to client2
    
    recv(acc1,line,strlen(line)+1,0);
    strcpy(line1,line);
    send(acc2,line1,strlen(line1)+1,0);
    
    //accept from client2 and send to clientt1
    
    recv(acc2,line,strlen(line)+1,0);
    strcpy(line1,line);
    send(acc1,line1,strlen(line1)+1,0);
    }
    
    while(strcpy(line,"quit"));
    
    close(acc1);
    close(acc2);
    return 0;
    
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    CLIENT1.c
    
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sock,n;
      char line[100],line1[200],sum[200];
      struct sockaddr_in client;
    
      sock=socket(AF_INET,SOCK_STREAM,0);
      if(sock==-1){
                   perror("unable to find the socket\n");
                   exit(1);
        }
    
     
      client.sin_family=AF_INET;
      client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      client.sin_port=htons(6000);
    
      bzero(&client.sin_zero,8);
    
    
      i=connect(sock,(struct sockaddr *)&client,sizeof(client));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    /* sending message to client1(your friend) */
    printf(" write message to your friend............\n: ");
    scanf("%s",line);
    strcpy(line1,line);
    send(sock,line1,strlen(line1)+1,0); 
    
    /* Receiving message from client1(your friend */
    n=recv(sock,line,strlen(line)+1,0);
            printf("message from your friend =%s\n",line);
    
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sock);
    
     return 0;
    }
    
    CLIENT2.c
    
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sock,n;
      char line[100],line1[200],sum[200];
      struct sockaddr_in client;
    
      sock=socket(AF_INET,SOCK_STREAM,0);
      if(sock==-1){
                   perror("unable to find the socket\n");
                   exit(1);
        }
    
    
      client.sin_family=AF_INET;
      client.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      client.sin_port=htons(6000);
    
      bzero(&client.sin_zero,8);
    
    
      i=connect(sock,(struct sockaddr *)&client,sizeof(client));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    /* sending message to client2(your friend) */
    printf("write message to your friend..........\n: ");
    scanf("%s",line);
    strcpy(line1,line);
    send(sock,line1,strlen(line1)+1,0); 
    
    /* Recieving message from client2(your friend */
    recv(sock,line,strlen(line)+1,0);
            printf("Message from your friend=%s\n",line);
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sock);
    
     return 0;
    }
    

    don’t forget to comment share and subscribe the site to get the latest update

  • Different Types of Patterns in shell script

    08/14/2019 by

    Welcome back!!!! Her in this version of our post we are going to see some tricky patterns in linux shell script or bash programming. I hope the code I have made for this pattern program is surly written in easy way for beginner as well as professional. This post included some of the most tricky patterns like star triangle, star pyramid ,inverted pyramid, alphabet triangle,alphabet pyramid,number triangle number pyramid and so on.

    Any way let us enjoy with code…………………………

    1.A SHELL SCRIPT TO DISPLAY TRIANGULAR PATTERN USING STAR(*)

    # A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size"
    read n
    
    for((i=1; i<=n; i++))
    do 
      for((j=1; j<=i;j++))
      do
        echo -n "* "
      done
      echo 
    done
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your triangular pattern is:
    * 
    * * 
    * * * 
    * * * * 
    * * * * * 
    * * * * * * 
    * * * * * * * 

    2. A SHELL SCRIPT TO DISPLAY INVERTED TRIANGULAR PATTERN USING STAR(*)

    # A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size"
    read n
    
    for((i=n; i>=1; i--)
    do 
      for((j=1; j<=i;j++))
      do
        echo -n "* "
      done
      echo 
    done
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your triangular pattern is:
    * * * * * * * 
    * * * * * * 
    * * * * * 
    * * * * 
    * * * 
    * * 
    * 
    

    3.A SHELL SCRIPT CODE TO DISPLAY TRIANGULAR NUMBER PATTERN

     #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size of your pattern"
    read n
    echo "your triangular pattern is:"
    for((i=1; i<=n; i++))
    do 
      for((j=1; j<=i;j++))
      do
        echo -n "$j "
      done
      echo 
    done
    
    
    
    
    
    
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your triangular pattern is:
    1 
    1 2 
    1 2 3 
    1 2 3 4 
    1 2 3 4 5 
    1 2 3 4 5 6 
    1 2 3 4 5 6 7 

    4.A SHELL SCRIPT CODE TO DISPLAY INVERTED NUMBER TRIANGLE

     #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size of your pattern"
    read n
    echo "your triangular pattern is:"
    for((i=n; i>=1; i--))
    do 
      for((j=1; j<=i;j++))
      do
        echo -n "$j "
      done
      echo 
    done
    
    
    
    
    
    
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your triangular pattern is:
    1 2 3 4 5 6 7 
    1 2 3 4 5 6 
    1 2 3 4 5 
    1 2 3 4 
    1 2 3 
    1 2 
    1 

    5.A SHELL SCRIPT CODE TO DISPLAY PASCAL’S TRIANGLE

    #A SHELL SCRIPT CODE TO PRINT PASCAL'S TRIANGLE
    
    #!/bin/sh
    coff=1
    echo "enter size of your triangle"
    read n
    for((i=0; i<=n; i++))
    do
      for((space=1; space<=n-i; space++))
      do
        echo -n " "
      done
      for((j=0; j<=i; j++))
      do
        if [[ $j == 0 || $i == 0 ]]
        then
            coff=1
       else
          coff=$((coff*(i-j+1)/j))
      fi
      echo -n $coff " "
    done
    echo " "
    done
    
    OUT PUT
    
    enter size of your triangle
    4
        1   
       1  1   
      1  2  1   
     1  3  3  1   
    1  4  6  4  1   

    6.A shell script code to display Floyd’s triangle

    
    # shell script to display Floyd's triangle
    
    #!/bin/sh
    k=1
    echo "enter the size of your pattern"
    read row
    echo "your Floyd's triangle is:"
    
    for((i=1; i<row; ++i))
    do
      for((j=1; j<=i; ++j))
      do
        echo -n "$k "
        ((++k))
      done
      echo
    done
    
    OUT PUT
    
    enter the size of your pattern
    5
    your Floyd's triangle is:
    1 
    2 3 
    4 5 6 
    7 8 9 10 

    7.A shell script code to display pyramid of pattern using star(*)

     #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size of your pattern"
    read n
    echo "your pyramidal pattern is:"
    for((i=1; i<=n; i++))
    do 
      for((space=1; space<=n-i; space++))
      do
         echo -n " "
      done
      for((j=1; j<=i; j++))
      do
        echo -n "* "
      done
      echo 
    done
    
    
    
    
    
    
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your pyramidal pattern is:
          * 
         * * 
        * * * 
       * * * * 
      * * * * * 
     * * * * * * 
    * * * * * * * 

    8.A shell script code to display inverted pyramidal pattern using star(*)

     #A SHELL SCRIPT TO DISPLAY STAR(*) TRIANGLE
    
    #!/bin/sh
    echo "enter size of your pattern"
    read n
    echo "your pyramidal pattern is:"
    for((i=n; i>=1; i--))
    do 
      for((space=1; space<=n-i; space++))
      do
         echo -n " "
      done
      for((j=1; j<=i; j++))
      do
        echo -n "* "
      done
      echo 
    done
    
    
    
    
    
    
    
    
    OUT PUT
    
    enter size of your pattern
    7
    your pyramidal pattern is:
    * * * * * * * 
     * * * * * * 
      * * * * * 
       * * * * 
        * * * 
         * * 
          * 
    

    9.A shell script code to display alphabet triangular pattern

    
    # shell scrip to display pyramid of alphabet
    
    #!/bin/sh
    alpha=( {A..Z} )
    echo "enter the size of your pattern"
    read row
    echo "alphabet pattern of size $row is:"
    
    for((i=1; i<=row; i++))
    do
    
       for((j=0; j<i; j++))
       do 
          echo -n "${alpha[j]} "
    
       done
       echo
    done
    
    OUT PUT
    
    enter the size of your pattern
    7
    pattern of size 7 is:
    A 
    A B 
    A B C 
    A B C D 
    A B C D E 
    A B C D E F 
    A B C D E F G 
    

    10.A shell script code to display inverted alphabet triangular pattern

    # shell scrip to display pyramid of alphabet
    
    #!/bin/sh
    alpha=( {A..Z} )
    echo "enter the size of your pattern"
    read row
    echo "alphabet pattern of size $row is:"
    
    for((i=row; i>=1; i--)
    do
    
       for((j=0; j<i; j++))
       do 
          echo -n "${alpha[j]} "
    
       done
       echo
    done
    
    OUT PUT
    
    enter the size of your pattern
    7
    pattern of size 7 is:
    A B C D E F G 
    A B C D E F 
    A B C D E 
    A B C D 
    A B C 
    A B 
    A 
    

    11.A shell script code to display alphabet pyramid

    
    # shell scrip to display pyramid of alphabet
    
    #!/bin/sh
    alpha=( {A..Z} )
    echo "enter the size of your pattern"
    read row
    echo "pattern of size $row is:"
    for((i=1; i<=row; i++))
    do
       for((space=1; space<=row-i; space++))
       do
          echo -n " "
       done
    
       for((j=0; j<i; j++))
       do 
          echo -n "${alpha[j]} "
    
       done
       echo
    done
    
    OUT PUT
    
    enter the size of your pattern
    7
    pattern of size 7 is:
          A 
         A B 
        A B C 
       A B C D 
      A B C D E 
     A B C D E F 
    A B C D E F G 
    

    12.A shell script code to display inverted alphabet of pyramid

    
    # shell scrip to display pyramid of alphabet
    
    #!/bin/sh
    alpha=( {A..Z} )
    echo "enter the size of your pattern"
    read row
    echo "pattern of size $row is:"
    for((i=row; i>=1; i--))
    do
       for((space=1; space<=row-i; space++))
       do
          echo -n " "
       done
    
       for((j=0; j<i; j++))
       do 
          echo -n "${alpha[j]} "
    
       done
       echo
    done
    
    OUT PUT
    
    enter the size of your pattern
    6
    pattern of size 6 is:
    A B C D E F 
     A B C D E 
      A B C D 
       A B C 
        A B 
         A 
    

    13.A shell script to display binary triangular number pattern

    finally I would like to show you one more unique pattern which is of binary number form. we can create any type of binary number pattern but now i will write one code to display mixed binary digit pattern which is little tricky the pattern looks like as the one given below. ya exactly it’s so beautiful let see the code also

    1 
    0 1 
    1 0 1 
    0 1 0 1 
    1 0 1 0 1 
    0 1 0 1 0 1 
    1 0 1 0 1 0 1 
    
    See the code below it seems difficult but chill it is just easy......
    
    # A SHELL SCRIPT CODE TO DISPLAY PATTERN
    # OF BINARY DIGIT 
    
    #!/bin/sh
    coff=1
    echo "enter size of your pattern"
    read n
    echo "The binary number pattern of size $n is:"
    for((i=1; i<=n; i++))
     do
       for((j=1; j<=i; j++))
       do
          if (( (i+j)%2==0 ))
          then
             echo -n "1 "
          else
             echo -n "0 "
        fi
       done
       echo
    done
    
    
    OUT PUT
    enter size of your pattern
    7
    The binary number pattern of size 7 is:
    
    1 
    0 1 
    1 0 1 
    0 1 0 1 
    1 0 1 0 1 
    0 1 0 1 0 1 
    1 0 1 0 1 0 1 
    

    14.A shell script to display inverted pattern of binary number

    Here I am going to show you the inverted binary number pattern in a triangular form which is given above in a normal form

    # A SHELL SCRIPT CODE TO DISPLAY PATTERN
    # OF BINARY DIGIT 
    
    #!/bin/sh
    coff=1
    echo "enter size of your pattern"
    read n
    echo "The binary number pattern of size $n is:"
    for((i=n; i>=1; i--)
     do
       for((j=1; j<=i; j++))
       do
          if (( (i+j)%2==0 ))
          then
             echo -n "1 "
          else
             echo -n "0 "
        fi
       done
       echo
    done
    
    OUT PUT
    
    enter size of your pattern
    7
    The inverted binary pattern of size 7 is:
    1 0 1 0 1 0 1 
    0 1 0 1 0 1 
    1 0 1 0 1 
    0 1 0 1 
    1 0 1 
    0 1 
    1 
    

    15.A shell script to display pyramidal binary number pattern

    Here also I am going to show you how to display the binary number format given above in a pyramidal format

    let’s go here is the code.

    # A SHELL SCRIPT CODE TO DISPLAY PATTERN
    # OF BINARY DIGIT 
    
    #!/bin/sh
    coff=1
    echo "enter size of your pattern"
    read n
    echo "The pyramidal binary pattern of size $n is:"
    for((i=1; i<=n; i++))
    do
       for((space=1; space<=n-i; space++))
       do
          echo -n " "
       done
    
       for((j=1; j<=i; j++))
       do
          if (( (i+j)%2==0 ))
          then
              echo -n "1 "
          else
             echo -n "0 "
         fi
        done
       echo
    done
    
    
    enter size of your pattern
    7
    The inverted binary pattern of size 7 is:
          1 
         0 1 
        1 0 1 
       0 1 0 1 
      1 0 1 0 1 
     0 1 0 1 0 1 
    1 0 1 0 1 0 1 
    

    16.A shell script to display an inverted pyramidal binary number pattern.

    Finally I would like to show you the binary number inverted pyramidal pattern which is the end of this post.

    # A SHELL SCRIPT CODE TO DISPLAY PATTERN
    # OF BINARY DIGIT 
    
    #!/bin/sh
    coff=1
    echo "enter size of your pattern"
    read n
    echo "The pyramidal binary pattern of size $n is:"
    for((i=n; i>=1; i--))
    do
       for((space=1; space<=n-i; space++))
       do
          echo -n " "
       done
    
       for((j=1; j<=i; j++))
       do
          if (( (i+j)%2==0 ))
          then
              echo -n "1 "
          else
             echo -n "0 "
         fi
        done
       echo
    done
    
    
    OUT PUT
    
    enter size of your pattern
    7
    The pyramidal binary pattern of size 7 is:
    1 0 1 0 1 0 1 
     0 1 0 1 0 1 
      1 0 1 0 1 
       0 1 0 1 
        1 0 1 
         0 1 
          1 
    

    Thanks for your comment. please don’t forget to share the site to your friends.

  • 6. C program to create a TCP socket in which three clients communicate to each other through a single server.

    08/08/2019 by

    In our previous post we have seen how two client can communicate to each other through a single server. But now upgrading the same concept we are going to see how three client can communicate to each other . In these post I am going to show you a TCP socket in which two client will send a number to the server and the server will add the two number and forward it to the third client. hopefully after these tutorial you all can extend the program for any number of client to be able to communicate over a single client.

    /*SERVER.c*/
    /*Here in the server we will receive the two number from client1 and client2. after adding the two number we will send the sum to client3*/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<sys/socket.h>
    #include<sys/types.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    
    
    int main(){
    int a,b,c,arr[100],acc1,acc2,acc3,sock,list,value,len,num1,num2,sum;
    char line[100],line1[100];
    struct sockaddr_in server,client;
    
    sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock==-1){
    perror("socket not found\n");
    exit(1);
    }
    
    server.sin_family=AF_INET;
    server.sin_port=htons(6000);
    server.sin_addr.s_addr=INADDR_ANY;
    bzero(&server.sin_zero,8);
    
    b=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(b==-1){
    perror("unable to bind\n");
    exit(1);
    }
    
    list=listen(sock,5);
    if(list==-1){
    perror("unable to listen to the client\n");
    exit(1);
    }
    while(1){
    printf("connection established waiting for the client\n");
    
    //accept1
    len=sizeof(struct sockaddr_in);
    acc1=accept(sock,(struct sockaddr *)&client,&len);
    if(acc1==-1){
    perror("acceptance error\n");
    exit(1);
    }
    //accept2
    acc2=accept(sock,(struct sockaddr *)&client,&len);
    if(acc2==-1){
    perror("acceptance error\n");
    exit(1);
    }
    //accept3
    acc3=accept(sock,(struct sockaddr *)&client,&len);
    if(acc3==-1){
    perror("acceptance error\n");
    exit(1);
    }
    
    printf("passing information between client.............\n");
    do{
    //accept from client1 
    a=recv(acc1,line,strlen(line)+1,0);
    num1=atoi(line);
    //accept from client2 
    a=recv(acc2,line,strlen(line)+1,0);
    num2=atoi(line);
    //send the sum to 3rd client
    sum=num1+num2;
    sprintf(line1,"%d\n",sum);
    c=send(acc3,line1,strlen(line1),0);
    }
    
    while(abs(strcpy(line,"quit")));
    
    close(acc1);
    close(acc2);
    return 0;
    
    }
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /*CLIENT1.c*/
    /*These client will send only an integer to the server but nothing to recieve*/
    
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sockfd,n;
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
      if(sockfd==-1){
        perror("socket connection failed\n");
      exit(1);
     } 
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    printf("enter 1st num to be sent\n: ");
    scanf("%s",line);
    send(sockfd,line,strlen(line)+1,0); 
    
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sockfd);
    
     return 0;
    }
    
    /*CLIENT2.c*/
    /*This client also will send an integer to the server nothing to receive*/
    
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sockfd,n;
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
      if(sockfd==-1){
        perror("socket connection failed\n");
      exit(1);
     } 
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    printf("enter 2st num to be sent\n: ");
    scanf("%s",line);
    send(sockfd,line,strlen(line)+1,0); 
    
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sockfd);
    
     return 0;
    }
    
    /*CLIENT3.c*/
    /*This client will receive the sum of the two number sent by clint1 and client2 where the server performs the addition*/
    
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sockfd,n;
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
      if(sockfd==-1){
        perror("socket connection failed\n");
      exit(1);
     } 
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    n=recv(sockfd,line,100,0);
            printf("sum recieved from client1 and client2  through server=%s\n",line);
    exit(1);
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sockfd);
    
     return 0;
    }
    

  • 4. A c.program to create TCP program through which a client send an integer and the server will reverse the number and forward it to the client.

    08/03/2019 by
    /*A SERVER CODE TO SORT AN ARRAY AND SEND IT TO THE CLIENT*/
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<unistd.h>
    #include<string.h>
    
    
    int main(){
    int sock,len,k,lis,acc,sent;
    char line[100],line1[100],line2[100];
    int num1,digit=0,rev=0,n;
    char buf[]="welcome to socket Programming";
    unsigned int rec;
    struct sockaddr_in server,client;
    
    
    sock=socket(AF_INET,SOCK_STREAM,0);
    
    if(sock==-1){
    perror("Socket\n");
    exit(1);
    }
    server.sin_family=AF_INET;
    server.sin_port=htons(6000);
    server.sin_addr.s_addr=INADDR_ANY;
    
    bzero(&server.sin_zero,8);
    
    len=sizeof(struct sockaddr_in);
    k=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(k==-1){
    perror("bind\n");
    exit(1);
    }
    
    lis=listen(sock,5);
    if(lis==-1){
    perror("listen\n");
    exit(1);
    }
    
    while(1){
    
    printf("waiting for client connection on port 6000\n");
    acc=accept(sock,(struct sockaddr *)&client,&len);
    if(acc==-1){
    perror("accept\n");
    exit(1);
    }
    
    
    printf("Passing Information to the client............................\n");
    
    memset(line,0x0,100);
    
    n=recv(acc,line,100,0);
    num1=atoi(line);
    
    while(num1!=0){
    rev=rev*10;
    rev=rev+(num1%10);
    num1=num1/10;
    }
    
    
    sprintf(line1,"%d\n",rev);
    
    send(acc,line1,strlen(line1),0);
    
    
    close(acc);
    return 0;
    
    }
    }

    4.2 CLIENT CODE FOR THE ABOVE SERVER PROGRAM

    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<string.h>
    int main()
    {
      int i,sockfd,n;
      char buf[100];
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
    printf("connection Established successfully continue your operation\n");
    
    /* After connection, the client can send or receive messages.*/
    do{
    printf("enter the number to be reversed: ");
    scanf("%s",line);
    send(sockfd,line,strlen(line)+1,0); 
    
    n=recv(sockfd,line,100,0);
            printf("Reverse number received from server=%s\n",line);
    }
    
    while(strcpy(line,"quite"));
    	
    close(sockfd);
    
     return 0;
    }

    Please leave your comment

    share and subscribe our site if you get some thing good.

  • 3. A c.program to create a TCP socket through which a client sends an array and the server sorts an array and forward it to the clients.

    08/03/2019 by
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<string.h>
    
    
    int main(){
    int sock,len,k,lis,acc,sent;
    char line[100],line1[100],line2[100];
    int num1,digit=0,rev=0,n,a[100],i,j;
    char buf[]="welcome to socket Programming";
    unsigned int rec;
    struct sockaddr_in server,client;
    
    
    sock=socket(AF_INET,SOCK_STREAM,0);
    
    if(sock==-1){
    perror("Socket\n");
    exit(1);
    }
    server.sin_family=AF_INET;
    server.sin_port=htons(6000);
    server.sin_addr.s_addr=INADDR_ANY;
    
    bzero(&server.sin_zero,8);
    
    len=sizeof(struct sockaddr_in);
    k=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(k==-1){
    perror("bind\n");
    exit(1);
    }
    
    lis=listen(sock,5);
    if(lis==-1){
    perror("listen\n");
    exit(1);
    }
    
    while(1){
    
    printf("waiting for client connection on port 6000\n");
    acc=accept(sock,(struct sockaddr *)&client,&len);
    if(acc==-1){
    perror("accept\n");
    exit(1);
    }
    
    
    printf("Passing Information to the client............................\n");
    
    memset(line,0x0,100);
    
    do{
    k=recv(acc,&a,5*sizeof(int),0);
    
    for(i=0;i<5;i++){
    for(j=i+1;j<5;j++){
    if(a[i]>a[j]){
    int temp=a[i];
    a[i]=a[j];
    a[j]=temp;
    }
    }
    }
    
    send(acc,&a,5*sizeof(int),0);
    }
    while(strcpy(line,"quit"));
    close(acc);
    return 0;
    
    }
    }

    A client code for the above server

    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    
    int main()
    {
      int i,sockfd,n,k,a[100],b[100];
      char buf[100];
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
    printf("connection Established successfully continue your operation\n");
    
    /* After connection, the client can send or receive messages.*/
    do{
    
       printf("enter 5 array element to be sorted:\n ");
       for(i=0;i<5;i++){
                scanf("%d",&a[i]);
    }
       send(sockfd,&a,5*sizeof(int),0); 
    
       k=recv(sockfd,&b,5*sizeof(int),0);
     
       printf("sorted array received from server\n");
       for(i=0;i<5;i++){
                 printf("%d\n ",b[i]);
    }
    } 
    while(strcpy(line,"quite"));
    	
    close(sockfd);
    
     return 0;
    }
  • A c.program to create a TCP socket through which a client will send two integer to the server and server will add the two integer and forward the sum to the client.

    08/03/2019 by

    1.2 SERVER CODE (SERVER.c)

    /*A server program to add two number sent from client and send back the sum*/
    
    #include<stdio.h>
    #include<unistd.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    
    
    int main(){
    int sock,len,k,lis,acc,sent;
    char line[100],line1[100],line2[100];
    int num1,num2,sum,n;
    char buf[]="welcome to socket Programming";
    unsigned int rec;
    struct sockaddr_in server,client;
    
    
    sock=socket(AF_INET,SOCK_STREAM,0);
    
    if(sock==-1){
    perror("Socket connection failed \n");
    exit(1);
    }
    server.sin_family=AF_INET;
    server.sin_port=htons(6000);
    server.sin_addr.s_addr=INADDR_ANY;
    
    bzero(&server.sin_zero,8);
    
    len=sizeof(struct sockaddr_in);
    k=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(k==-1){
    perror("bind error \n");
    exit(1);
    }
    
    lis=listen(sock,5);
    if(lis==-1){
    perror("listen\n");
    exit(1);
    }
    
    while(1){
    
    printf("waiting for client connection on port 6000\n");
    acc=accept(sock,(struct sockaddr *)&client,&len);
    if(acc==-1){
    perror("accept denied \n");
    exit(1);
    }
    
    do{
    printf("Passing Information to the client............................\n");
    
    memset(line,0x0,100);
    
    n=recv(acc,line,100,0);
    num1=atoi(line);
    
    n=recv(acc,line,100,0);
    num2=atoi(line);
    
    sum=num1+num2;
    sprintf(line1,"%d\n",sum);
    
    send(acc,line1,strlen(line1),0);
    }
    
    while(abs(strcpy(line,"quit")));
    
    
    
    close(acc);
    return 0;
    
    }
    }

    1.2.1 Now it’s time to proceed to client code (CLIENT.c)

    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<unistd.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    int main()
    {
      int i,sockfd,n;
      char line[100],num2[200],sum[200];
      struct sockaddr_in sa_addr;
    
      sockfd=socket(AF_INET,SOCK_STREAM,0);
      if(sockfd==-1){
        perror("socket connection failed\n");
      exit(1);
     } 
    
    
      sa_addr.sin_family=AF_INET;
      sa_addr.sin_addr.s_addr=inet_addr("127.0.0.1"); //Loop back IP address
      sa_addr.sin_port=htons(6000);
    
      bzero(sa_addr.sin_zero,8);
    
    
      i=connect(sockfd,(struct sockaddr *)&sa_addr,sizeof(sa_addr));
      if(i==-1){
        perror("unable to find the server\n");
       }
      else{
         printf("connection Established successfully continue your operation\n");
      }
    /* After connection, the client can send or receive messages.*/
    do{
    
    printf("enter 1st number: ");
    scanf("%s",line);
    send(sockfd,line,strlen(line)+1,0); 
    
    printf("enter 2nd number: ");
    scanf("%s",line);
    send(sockfd,line,strlen(line)+1,0);
    
    n=recv(sockfd,line,100,0);
            printf("sum received from server=%s\n",line);
    
    }
    while(strcpy(line,"quit"));
    
    
    	
    close(sockfd);
    
     return 0;
    }

    please leave your comment if get some thing using useful.

  • Socket Programming

    08/03/2019 by

    Here we are going to deal with some basic TCP socket programming using c

    Under this version of my post I am going to provide you some important socket codes with explanation hoping that reader has some concept of computer network as prerequisite so that easily understand can the code.

    1.A simple TCP program in c in which server and client communicate to each other (server_client_Interaction)

    /*SERVER.c*/
    /*NETWORK PROGRAMMING WITH SOCKETS FOR SERVER*/
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    #include<unistd.h>
    #include<stdlib.h>
    int main()
    {
    int sock, length,i,list;
    unsigned int acc;
    char line1[100],line2[100];
    struct sockaddr_in server,client;
    
    /*calling socket function*/
    sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock==-1){
               perror("unable to find the socket\n");
               exit(1);
    }
     /*already defined member in sockaddr_in structure*/
     server.sin_family=AF_INET;
     server.sin_addr.s_addr=INADDR_ANY;
     server.sin_port=htons(6000);
    
     bzero(&server.sin_zero,8);
    
    /*calling bind function*/
     i=bind(sock,(struct sockaddr *)&server,sizeof(server));
    if(i==-1){
               perror("binding error\n");
               exit(1);
    }
    
    /*liten function calling ro listen to the client*/
     list=listen(sock,5);
     if(list==-1){
               perror("listeninig error\n");
               exit(1);
    }
    else{
        printf("Operation success waiting for client to connect on port 6000\n");
    }
    
    while(1){
     /*Accept function to grant acceptance*/
     length=sizeof(client);
     acc=accept(sock, (struct sockaddr *) &client,&length);
     if(acc==-1){
                perror("acceptance error\n");
               exit(1);
    }
     /*Message to client*/
    strcpy(line1,"Message from Server");
    strcpy(line2,"Welcome to Socket Programming");
    send(acc, line1, 100, 0);
    send(acc, line2, 100, 0);
    
    /*Message from client*/
    recv(acc, line1, 100, 0);
    printf("%s\n", line1);
    
    close(acc);
    
    return 0;
    
    }
    }

    In the above code we have written the server program with comment line explanation for each and every pre defined function if you have any problem in understanding please put your comment …… and don”t forget to share subscribe the site if you get some thing useful.

    1.1 Client code for simple communication for the server code written above.

    /*CLIENT.c*/
    /*COMPUTER NETWORK OF THE CLIENT PROCESS*/
    
    
    
    #include<stdio.h>
    #include<sys/types.h>
    #include<sys/socket.h>
    #include<netinet/in.h>
    #include<arpa/inet.h>
    #include<fcntl.h>
    #include<string.h>
    #include<stdlib.h>
    #include<unistd.h>
    int main()
    {
      int i,sock;
      char line1[100];
      struct sockaddr_in client;
      
    /*calling socket function*/
      sock=socket(AF_INET,SOCK_STREAM,0);
    if(sock==-1){
               perror("unable to find the socket\n");
               exit(1);
    }
    
    /*already defined member in sockaddr_structure*/
      client.sin_family=AF_INET;
      client.sin_addr.s_addr=inet_addr("127.0.0.1");    /*Loop back IP address*/   
      client.sin_port=htons(6000);   /*port number for communication must be same with server port number*/
    
     bzero(&client.sin_zero,8);
    
    /*connect function calling*/
      i=connect(sock,(struct sockaddr *)&client,sizeof(client));
    if(i==-1){
               perror("connection error please try again\n");
               exit(1);
    }
    else{
        printf("Congra!!! connected to server successfully\n");
    }
    /* After connection, the client can send or receive messages.*/
    
    /*receiving message from server*/
    	recv(sock, line1, 100, 0);
    	printf("%s\n", line1);
    /*sending message to server*/
    
    	strcpy(line1,"Message from client");
    	send(sock, line1, 100, 0);
    	
    close(sock);
    return 0;
    
     
    }

    Now after writing the code on linux operating system (ubutu editor)

    open two window to run the server and client then compile both program then absolutely you will be satisfied that easily using one system you can do every thing as client and server simultaneously using loop back ip_address (127.0.0.1) as given in the code.

    thanks for visiting us

    your comment is a building block for our site

  • A Java Program To display diffrent window background using Frame class

    07/26/2019 by
    /*By clicking on the button you can set your window background on your desired color*/
    /*I recommend you this code if your eclipse doesn't support applet viewer 
    else refer to our applet code for window background which is more easer*/
    
    package p1;
    
    
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class Background extends Frame implements ActionListener {
    	Button b1,b2,b3,b4;
    	String s="Please click the button for new Background",s2;
    	Font f1,f2,f3;
    Background() {
    	setLayout(new FlowLayout());
    	f1 = new Font("Arial",Font.BOLD,18);    
        f2 = new Font("Forte",Font.PLAIN,24);    
        f3 = new Font("Elephant",Font.ITALIC,28);
    	b1=new Button("Blue");
    	b2=new Button("Green");
    	b3=new Button("Cyan");
    	b4=new Button("Red");
    	add(b1);
    	add(b2);
    	add(b3);
    	add(b4);
    	b1.addActionListener(this);
    	b2.addActionListener(this);
    	b3.addActionListener(this);
    	b4.addActionListener(this);
    }
    public void paint(Graphics g) {
    	g.setFont(f1);
    	g.drawString(s,30,40);
    	g.setFont(f3);
    	g.drawString(s2,150,250);
    }
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		String k=e.getActionCommand();
    		if(k.equals("Blue")) {
    			setBackground(Color.blue);
    			s2="This is Blue Color Background";
    		}
    		if(k.equals("Green")) {
    			setBackground(Color.green);
    			s2="This is Green Color Background";
    		}
    		if(k.equals("Cyan")) {
    			setBackground(Color.cyan);
    			s2="This is Cyan Color Background";
    		}
    		if(k.equals("Red")) {
    			setBackground(Color.red);
    			s2="This is Red Color Background";
    		}
    			
    		}
    	public static void main(String args[]) {
    		Background b=new Background();
    		b.setSize(new Dimension(200,300));
    		b.setVisible(true);
    	}
    		
    		
    	}
    	
    
    
    
  • A Java Program to display diffrent windows Background using an applet class

    07/26/2019 by
    /*By clicking on the button you can change your windows backgraound as your wish*/
    
    package p1;
    
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class SetBackground extends Applet implements ActionListener {
    	Button b1,b2,b3,b4;
    	String s="Please click the button for new Background",s2;
    	Font f1,f2,f3;
    public void init() {
    	f1 = new Font("Arial",Font.BOLD,18);    
        f2 = new Font("Forte",Font.PLAIN,24);    
        f3 = new Font("Elephant",Font.ITALIC,28);
    	b1=new Button("Blue");
    	b2=new Button("Green");
    	b3=new Button("Cyan");
    	b4=new Button("Red");
    	add(b1);
    	add(b2);
    	add(b3);
    	add(b4);
    	b1.addActionListener(this);
    	b2.addActionListener(this);
    	b3.addActionListener(this);
    	b4.addActionListener(this);
    }
    public void paint(Graphics g) {
    	g.setFont(f1);
    	g.drawString(s,30,40);
    	g.setFont(f3);
    	g.drawString(s2,150,250);
    }
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		// TODO Auto-generated method stub
    		String k=e.getActionCommand();
    		if(k.equals("Blue")) {
    			setBackground(Color.blue);
    			s2="This is Blue Color Background";
    		}
    		if(k.equals("Green")) {
    			setBackground(Color.green);
    			s2="This is Green Color Background";
    		}
    		if(k.equals("Cyan")) {
    			setBackground(Color.cyan);
    			s2="This is Cyan Color Background";
    		}
    		if(k.equals("Red")) {
    			setBackground(Color.red);
    			s2="This is Red Color Background";
    		}
    			
    		}
    		
    		
    	}
  • A Java program to constraint your password with letter,special character,number for varification

    07/26/2019 by
    import java.util.Scanner;
    public class Password3 {
    
    public static void main(String[] args) {
        int min =8;
        int max=16;
        int digit=0;
        int special=0;
        int upCase=0;
        int loCase=0;
        String password;
        Scanner sc = new Scanner(System.in);
        System.out.println(" Enter Your Password:");
            password = sc.nextLine();
            for(int i =0;i<password.length();i++){
    		if(password.length()>=min&&password.length()<=max){
                char c = password.charAt(i);
                if(Character.isUpperCase(c)){
                    upCase++;
                }
                if(Character.isLowerCase(c)){
                    loCase++;
                }
                if(Character.isDigit(c)){
                    digit++;
                }
                if(c>=33&&c<=46||c==64){
                    special++;
                }
    		}
    		}
            if(special>=1&&loCase>=1&&upCase>=1&&digit>=1){
                System.out.println(" Password is perfectly match you can procced:" +password);
    		}
    
    else{
    	if(password.length()<min){
    		System.out.println("1. Password must be atleat "+min+" characters:");
    	}
    	if(password.length()>max){
    		System.out.println("2. Password must be less than "+max+" characters:");
    	}
    	if(special<1){
    		System.out.println("3. You need atleast one special chracter:");
    	}
    	if(digit<1){
    		System.out.println("4. You need atleast one digit:");
    	}
    	if(upCase<1){
            System.out.println("5. You need atleast one upper case:");
    	}
    		if(loCase<1){
            System.out.println("6. You need atleast one lower case:");
              }
            }
    	}
    }
    

    Follow My Blog

    Get new content delivered directly to your inbox.

View all posts

A Java program to constraint your password with letter,special character,number for varification

import java.util.Scanner;
public class Password3 {

public static void main(String[] args) {
    int min =8;
    int max=16;
    int digit=0;
    int special=0;
    int upCase=0;
    int loCase=0;
    String password;
    Scanner sc = new Scanner(System.in);
    System.out.println(" Enter Your Password:");
        password = sc.nextLine();
        for(int i =0;i<password.length();i++){
		if(password.length()>=min&&password.length()<=max){
            char c = password.charAt(i);
            if(Character.isUpperCase(c)){
                upCase++;
            }
            if(Character.isLowerCase(c)){
                loCase++;
            }
            if(Character.isDigit(c)){
                digit++;
            }
            if(c>=33&&c<=46||c==64){
                special++;
            }
		}
		}
        if(special>=1&&loCase>=1&&upCase>=1&&digit>=1){
            System.out.println(" Password is perfectly match you can procced:" +password);
		}

else{
	if(password.length()<min){
		System.out.println("1. Password must be atleat "+min+" characters:");
	}
	if(password.length()>max){
		System.out.println("2. Password must be less than "+max+" characters:");
	}
	if(special<1){
		System.out.println("3. You need atleast one special chracter:");
	}
	if(digit<1){
		System.out.println("4. You need atleast one digit:");
	}
	if(upCase<1){
        System.out.println("5. You need atleast one upper case:");
	}
		if(loCase<1){
        System.out.println("6. You need atleast one lower case:");
          }
        }
	}
}

Follow My Blog

Get new content delivered directly to your inbox.

Design a site like this with WordPress.com
Get started