TCP Client-Server Program - Check Palindrome - Network-DBMS Lab - C program

Program:

// Client Program: pltcpc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
   struct sockaddr_in client;
   int s,flag;
   char buffer[20];
   s=socket(AF_INET,SOCK_STREAM,0);
   client.sin_family=AF_INET;
   client.sin_port=2000;
   client.sin_addr.s_addr=inet_addr("127.0.0.1");
   connect(s,(struct sockaddr *)&client,sizeof client);
   for(;;)
    {
    printf("\nEnter a string to check palindrome: ");
    scanf("%s",buffer);
    printf("\nClient: %s",buffer);
    send(s,buffer,sizeof(buffer),0);
    recv(s,&flag,sizeof(int),0);
    if(flag==1)
    {
      printf("\nServer: The string is a Palindrome.\n");
        break;
    }
    else
    {
       printf("\nServer: The string is not a palindrome.\n");
        break;
    }
     }
    close(s);
}

// Server program: pltcps.c

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

main()
{
  struct sockaddr_in client,server;
  int s,n,sock,g,j,left,right,flag;
  char b1[20],b2[10],b3[10],b4[10];
  s=socket(AF_INET,SOCK_STREAM,0);
  server.sin_family=AF_INET;
  server.sin_port=2000;
  server.sin_addr.s_addr=inet_addr("127.0.0.1");
  bind(s,(struct sockaddr *)&server,sizeof server);
  listen(s,1);
  n=sizeof client;
  sock=accept(s,(struct sockaddr *)&client,&n);
  for(;;)
   {
      recv(sock,b1,sizeof(b1),0);
      printf("\nThe string received is:%s\n",b1);
      if(strlen(b1)==0)
    flag=1;
      else
      {
    left=0;
        right=strlen(b1)-1;
        flag=1;
        while(left<right && flag)
        {
        if (b1[left]!= b1[right])
        flag=0;
        else
        {
            left++;
            right--;
        }
        }
      }
      send(sock,&flag,sizeof(int),0);
      break;
   } 
  close(sock);
  close(s);
}

Output :

Terminal 1: (Client)
nn@linuxmint ~ $ gcc pltcpc.c -o client
nn@linuxmint ~ $ ./client

Enter a string to check palindrome: malayalam

Client: malayalam
Server: The string is a Palindrome.
nn@linuxmint ~ $

Terminal 2: (Server)
nn@linuxmint ~ $ gcc pltcps.c -o server
nn@linuxmint ~ $ ./server

The string received is:malayalam
nn@linuxmint ~ $


5 comments:

  1. I dont get the output for this program,
    Can you please give me code to do arithmetic operations by client server programming.

    Please ASAP
    THANK YOU

    ReplyDelete
  2. I didnt get output for this code.. can any one help me... u can mail me the code to sumiyashaikh@gmail.com.. thanks

    ReplyDelete
  3. I didnt get output for this code.. can any one help me... u can mail me the code to sumiyashaikh@gmail.com.. thanks

    ReplyDelete
  4. Have you a c code that do arithmetic operation ?

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...