Add to Favorites    Make Home Page 2506 Online  
 Language Categories  
 Our Services  

Home » C Home » Beginners / Lab Assignments Home » Network: File manipulating program


Search Projects & Source Codes:

Title Network: File manipulating program
Author sam
Author Email sa_jal1982 [at]
Description One file from server is copied and edited RESULTING in two files. Those two files are sent to client, which are further edited resulting
in four files.
Category C » Beginners / Lab Assignments
Hits 368599
Code Select and Copy the Code
Code : //------------------------SERVER---------------------------------- //server: opens a file and write into another file #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<arpa/inet.h> #include<unistd.h> #define SIZE 2000 #define PORT 11 int main(int argc,char *argv[]) { FILE *X, *Y; int sockfd, client_sockfd; unsigned int nt, len; struct sockaddr_in serid, clid; char x[5],y[5], fd[SIZE], buf[SIZE]; int g,siz = 2000; char ch,s[siz],fn[siz],con[siz]; sockfd = socket(AF_INET, SOCK_STREAM, 0); printf("sock created "); //////// file1 printf(" enter the file name : name = "); scanf("%s",x); X=fopen(x,"r"); if(X == NULL) { printf(" file open error "); exit(0); } fread(con,(size_t)sizeof(char),(size_t)SIZE , X); printf(" X = %s",con); ///////////file2 printf(" ______________________________________________"); printf(" enter the file name:-"); scanf("%s",argv[0]); // Y=fopen("y.t","a+"); strcpy(y,argv[0]); Y=fopen(y,"w"); if( Y == NULL) { puts(" cannot open the file"); exit(0); } fputs(con,Y); char text[20];//=" I I Sc-Bangalore "; printf(" enter the text to edit: text = "); scanf(" %s",text); // gets(text); fputs(text,Y); fclose(Y); Y=fopen(y,"a+"); fread(s,(size_t)sizeof(char),(size_t)SIZE , Y); printf(" Y = %s ",s); fclose(X); fclose(Y); ////////////////// sockets serid.sin_family = AF_INET; serid.sin_addr.s_addr =inet_addr("");//INADDR_ANY;//""; serid.sin_port=htons(PORT);//"80"; printf(" server "); if((bind(sockfd,(struct sockaddr *) &serid, sizeof(struct sockaddr_in))) < 0 ) { printf("bind error "); exit(0); } //////// specify queue listen(sockfd, 5); while(1) { len = sizeof(struct sockaddr_in); //printf("waiting"); if((client_sockfd = accept(sockfd,(struct sockaddr *) &clid, &len) ) < 0 ) { printf(" accept error "); exit(0); } 73,0-1 72% //////////file1 write(client_sockfd,x,sizeof(con)); read(client_sockfd,buf,sizeof(buf)); X=fopen(x,"r"); if(X == NULL) { printf(" file open error "); exit(0); } fread(con,(size_t)sizeof(char),(size_t)SIZE , X); write(client_sockfd,con,sizeof(con)); fclose(X); //////file2 read(client_sockfd,buf,sizeof(buf)); write(client_sockfd,y,sizeof(s)); read(client_sockfd,buf,sizeof(buf)); Y=fopen(x,"r"); fread(s,(size_t)sizeof(char),(size_t)SIZE , Y); write(client_sockfd,s,sizeof(s)); //fclose(X); fclose(Y); close(client_sockfd); } } //-------------------------CLIENT---------------------------------- //client reads two files from server and edit it saves in other files as a.t and b.t #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define SIZE 2000 #define PORT 1111 //char buf[SIZE]; int main(int argc, char *argv[]) { int sockfd; int t,nt,siz=2000; struct sockaddr_in serv_addr; //unsigned int SIZE =1000; char buf[SIZE],buf2[SIZE]; unsigned int filesize=100; char fd[siz],new[siz],edt[siz],var[siz],var2[siz]; char fd2[siz],new2[siz],edt2[siz]; //unsigned int siz=5; char fn[5]; printf(" ---client-- "); sockfd = socket(AF_INET, SOCK_STREAM, 0); serv_addr.sin_family=AF_INET; serv_addr.sin_addr.s_addr=inet_addr("");//INADDR_ANY;//"10.32.2 1.33"; serv_addr.sin_port=htons(PORT);//"80"; connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); ///////////////////////////first file///////////////////////////////////////////////// read(sockfd, buf,sizeof(buf)); write(1,buf,7); write(sockfd,buf,sizeof(buf)); read(sockfd,var,sizeof(var)); FILE *fdd = fopen(buf,"w"); if(fdd == NULL){ printf(" file open error "); exit(0); } //printf(" %s",var); fputs(var, fdd); fclose(fdd); fdd=fopen(buf,"r"); nt=fread(fd, (size_t)sizeof(char),(size_t)SIZE , fdd); printf(" %s=%s ",buf,fd); printf(" ----------------------"); FILE *NEW_X=fopen("a.t","w"); if(NEW_X == NULL) { printf(" can't open the file "); exit(0); } fputs(fd,NEW_X); // fclose(NEW_X); char s[20]; printf(" enter the text to add: text= "); scanf("%s",s); fputs(s,NEW_X); fgets(var,50,NEW_X); fclose(NEW_X); NEW_X=fopen("a.t","a+"); t=fread(new, (size_t)sizeof(char),(size_t)SIZE ,NEW_X); printf(" edited=%s ",new); ///////////////////////////second file //////////////////////////////////////////////////// printf(" --*****************************---- "); printf(" Request for second file "); scanf("%s",fn); //t=strlen(fn); printf(" ----************************--- "); write(sockfd,fn,sizeof(fn)); read(sockfd, buf2,sizeof(buf2)); write(1, buf2,5);//sizeof(buf2)); write(sockfd,buf2,sizeof(buf2)); read(sockfd,var2,sizeof(var2)); FILE *f2 = fopen(buf2,"w"); if(f2 == NULL){ printf(" file open error "); exit(0); } fputs(var2,f2); fclose(f2); f2=fopen(buf2,"r"); fread(fd2, (size_t)sizeof(char),(size_t)SIZE , f2); printf(" %s=%s ",buf2,fd2); printf(" --------------------------------------"); FILE *f3=fopen("b.t","w"); if(f3 == NULL) { printf(" can't open the file "); exit(0); } // t=fread(new2, (size_t)sizeof(char),(size_t)SIZE ,f3); // printf(" new=%s ",new2); fputs(fd2,f3); char s2[20];//=" UBDTCE"; printf(" enter the text to add: text = "); scanf("%s",s2); fputs(s2,f3); fclose(f3); f3=fopen("b.t","a+"); fread(edt2, (size_t)sizeof(char), (size_t)SIZE ,f3); printf(" edited = %s ",edt2); // write(1,edt,100);//sizeof(fd)); fclose(NEW_X); fclose(fdd); fclose(f3); fclose(f2); close(sockfd); //exit(0); } 2,85 Top

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy


Google Groups Subscribe to SourceCodesWorld - Techies Talk

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!

ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page


Google Search


Source Codes is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024, All Rights Reserved.
Page URL:

Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review