String Search and Replacement - Client-Server Program - Network-DBMS Lab - C Program

Program:

// Server : strs.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>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,choice=0,ack=0;
    char buffer[20],string[20],substring[20];
    FILE *fp,*temp;
   
    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");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
    recv(sock,string,sizeof(string),0);
    printf("String recieved: %s",string);
    found=0;
    if((fp=fopen("testrecord.txt","r"))==NULL)
    {
        printf("! The file is empty...\n");
        send(sock,&found,sizeof(int),0);
    }
    else
    {
        while(!feof(fp)&&found==0)
                {
                    fscanf(fp,"%s",buffer);
                    if(strcmp(buffer,string)==0)
                    {
                        found=1;
                printf("\nThe record found.\n");
            }
        }
        send(sock,&found,sizeof(int),0);
        fclose(fp);
    }
   
   
    recv(sock,&choice,sizeof(int),0);
    if(choice)
    {
       
        recv(sock,substring,sizeof(substring),0);
        printf("\nString Replacement:\nSubstring: %s",substring);
        temp=fopen("temp.txt","w");
        if((fp=fopen("testrecord.txt","r"))==NULL)
        {
            printf("! The file is empty...\n");
            send(sock,&ack,sizeof(int),0);
        }
        else
        {
           
            while(!feof(fp))
                {
                    fscanf(fp,"%s",buffer);
                           if(strcmp(buffer,string)==0)
                           {
                               fprintf(temp,"%s ",substring);
                    ack=1;                                               
                }
                else
                {
                    fprintf(temp,"%s ",buffer);
                }
            }
            send(sock,&ack,sizeof(int),0);
        }
        fclose(fp);
        fclose(temp);
        remove("testrecord.txt");
                rename("temp.txt","testrecord.txt");
                remove("temp.txt");
    }
    else
    {
        printf("\nNo replacement... \n");
            exit(0);
    }   
   
    close(sock);
    close(s);
}

// Client: strc.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>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],string[20],substring[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));
   
    printf("Enter a string to search in server:");
    scanf("%s",string);
    send(s,&string,sizeof(string),0);
    recv(s,&found,sizeof(int),0);
    if(found)
    {
        printf("\nThe record found.");
    }
    else
    {
            printf("\nNot found...\n");
            exit(0);
    }
   
    printf("\nDo you want to replace it ?\nEnter 1 -> Yes 2 -> No\n");
    scanf("%d",&choice);
    send(s,&choice,sizeof(int),0);
    if(choice)
    {
        printf("\nSubstring: ");
        scanf("%s",substring);
        send(s,&substring,sizeof(substring),0);
        recv(s,&ack,sizeof(int),0);
        if(ack)
        {
                printf("\nSuccess... \n");
        }
        else
        {
                printf("\nError... \n");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt

BAJAJ YAMAHA HONDA HEROHONDA TVS SUZUKI

Output:

Terminal 1: (Server)
nn@linuxmint ~ $ gcc strs.c -o server
nn@linuxmint ~ $ ./server
String recieved: HEROHONDA
The record found.

String Replacement:
Substring: HEROnn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc strc.c -o client
nn@linuxmint ~ $ ./client
Enter a string to search in server:HEROHONDA

The record found.
Do you want to replace it ?
Enter 1 -> Yes 2 -> No
1

Substring: HERO

Success... nn@linuxmint ~ $

// testrecord.txt

BAJAJ YAMAHA HONDA HERO TVS SUZUKI

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...