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

Program:

// Server: sbstrs.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,bi1=0;
    char buffer[100],substring[20],newstring[20];
    FILE *ptr,*op;
   
    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");
    op=fopen("result.txt","w");
    while((buffer[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buffer[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buffer[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            fprintf(op,"%s",buffer);
           
        }
        else
        {
            fprintf(op,"%s",newstring);printf("\nString replaced.");
        }
        bi1=0;bzero(buffer,100);
       
    }
   
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    fclose(op);
    remove("testrecord.txt");
        rename("result.txt","testrecord.txt");
        remove("result.txt");
        printf("\nSaved Successfully.\n");
    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],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... ");
        }
        else
        {
                printf("\nError... ");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt
    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
2K8618
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

Output:

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

Substring received: 2K8618.
Newstring received: NIDHEESH
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $

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

Enter the Newstring: NIDHEESH

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

// testrecord.txt

    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
NIDHEESH
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...