Substring Search & Replacement 1 - Client Server Program - Network-DBMS Lab - C Program

Program:

// Server: sbstrs1.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,ack=0,length=0,i,j,bi2=0,bi1=0;
    char buf1[200],buf2[200],substring[20],newstring[20];
    FILE *ptr,*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,substring,sizeof(substring),0);
    printf("\nSubstring received: %s.",substring);
    recv(sock,newstring,sizeof(newstring),0);
    printf("\nNewstring received: %s.",newstring);
   
    ptr=fopen("testrecord.txt","r");
    while((buf1[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buf1[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buf1[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            for(i=0;i<bi1;i++)
            {
                buf2[bi2]=buf1[i];
                bi2++;
            }
           
        }
        else
        {
            for(i=0;i<=strlen(newstring)-1;i++)
                    {
                        buf2[bi2]=newstring[i];
                bi2++;
            }
            printf("\nReplaced.\n");
        }
        bi1=0;
    }
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    temp=fopen("temp.txt","w");
           fprintf(temp,"%s",buf2);
    fclose(temp);
    remove("testrecord.txt");
        rename("temp.txt","testrecord.txt");
        remove("temp.txt");
    close(sock);
    close(s);
}


// Client: sbstrc.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],substring[20],newstring[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 the Substring: ");
    scanf("%s",substring);
    printf("\nEnter the Newstring: ");
    scanf("%s",newstring);
    send(s,&substring,sizeof(substring),0);

    send(s,&newstring,sizeof(newstring),0);
   
    recv(s,&ack,sizeof(int),0);

    if(ack)
    {
        printf("\nSubstring found & Replaced...\n");
    }
    else
    {
            printf("\nNot found...\n");
    }
    close(s);

}

// testrecord.txt

hi, goodnight...
ok.

Output:

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

Substring received: night.
Newstring received: morning.
Substring found.
Replaced.
nn@linuxmint ~ $

// Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbstrc.c -o client
nn@linuxmint ~ $ ./client
Enter the Substring: night

Enter the Newstring: morning

Substring found & Replaced...
nn@linuxmint ~ $


// testrecord.txt
hi, goodmorning...
ok.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...