Program:
// Server : sbs.c
// Client: sbc.c
// testrecord.txt
i like www.2k8618.blogspot.com.
Output:
Terminal 1: (Server)
nn@linuxmint ~ $ gcc sbs.c -o server
nn@linuxmint ~ $ ./server
Substring received: 2k8618.blogspot.com.
Newstring received: 2k8cse.cu.cc
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $
Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbc.c -o client
nn@linuxmint ~ $ ./client
Enter the substring: 2k8618.blogspot.com
Newstring: 2k8cse.cu.cc
Substring found & replaced.
nn@linuxmint ~ $
// testrecord.txt
i like www.2k8cse.cu.cc.
// Server : sbs.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_DGRAM,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)); recvfrom(s,substring,sizeof(substring),0,(struct sockaddr *)&client,&sz); printf("\nSubstring received: %s.",substring); recvfrom(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,&sz); 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); } sendto(s,&ack,sizeof(int),0,(struct sockaddr *)&client,sz); if(ack) { remove("testrecord.txt"); rename("result.txt","testrecord.txt"); printf("\nSaved Successfully.\n"); } remove("result.txt"); fclose(ptr); fclose(op); close(s); }
// Client: sbc.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],newstring[20],substring[20]; s=socket(AF_INET,SOCK_DGRAM,0); client.sin_family=AF_INET; client.sin_port=2000; client.sin_addr.s_addr=inet_addr("127.0.0.1"); printf("Enter the substring: "); scanf("%s",substring); printf("\nNewstring: "); scanf("%s",newstring); sendto(s,substring,sizeof(substring),0,(struct sockaddr *)&client,sizeof(client)); sendto(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,sizeof(client)); recvfrom(s,&found,sizeof(int),0,NULL,NULL); if(found) { printf("\nSubstring found & replaced.\n"); } else { printf("\nNot found.\n"); } close(s); }
// testrecord.txt
i like www.2k8618.blogspot.com.
Output:
Terminal 1: (Server)
nn@linuxmint ~ $ gcc sbs.c -o server
nn@linuxmint ~ $ ./server
Substring received: 2k8618.blogspot.com.
Newstring received: 2k8cse.cu.cc
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $
Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbc.c -o client
nn@linuxmint ~ $ ./client
Enter the substring: 2k8618.blogspot.com
Newstring: 2k8cse.cu.cc
Substring found & replaced.
nn@linuxmint ~ $
// testrecord.txt
i like www.2k8cse.cu.cc.
0 comments:
Post a Comment