Reading from a file in C++ and sending to socket until eof -
i trying read file , write output socket in c++. when trying compile code 2 errors. 1
#include <cassert> #include <arpa/inet.h> #include <cstdlib> #include <iostream> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <errno.h> #include <string.h> #include <fstream> #include <time.h> #include <strings.h> #include <sstream> #include <vector> #include <netdb.h> #include <netinet/in.h> #include <sys/sendfile.h> #include <fcntl.h> using namespace std; using std::endl; int newscount = 0; int doccount = 0; int photocount = 0; int johns = 0; vector<string> newsvector; vector<string> photosvector; vector<string> docsvector; void serverlogs(const char*msg) /* writing log file */ { time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime(&rawtime); ofstream file; file.open("serverlog.txt",ios::app); if (file.is_open()) {file<<msg; file<<" @ "; file<<asctime(timeinfo); file<<"\n"; file.close(); return; } else cout<<"error creating log file"; return; } void clientlogs(const char*msg) /* writing log file */ { time_t rawtime; struct tm * timeinfo; time (&rawtime); timeinfo = localtime(&rawtime); ofstream file; file.open("clientlog.txt",ios::app); if (file.is_open()) {file<<msg; file<<" @ "; file<<asctime(timeinfo); file<<"\n"; file.close(); return; } else cout<<"error creating log file"; return; } void error(const char*msg) /* if there error exit program err# 1 */ { perror(msg); exit(1); } void publishdocsvector(const char*msg) { docsvector.push_back(msg); ofstream file; file.open("docfiles.txt",ios::app); if (file.is_open()) { for(int j = 0; j < docsvector.size() ;j++) {file<<docsvector[j];} file.close(); } else cout<<"error creating news archive"; return; } void publishphotosvector(const char*msg) { photosvector.push_back(msg); ofstream file; file.open("photofiles.txt",ios::app); if (file.is_open()) { for(int j = 0; j < photosvector.size() ;j++) {file<<photosvector[j];} file.close(); } else cout<<"error creating news archive"; return; } void publishnewsvector(const char*msg) { newsvector.push_back(msg); ofstream file; file.open("newsfiles.txt",ios::app); if (file.is_open()) { for(int j = 0; j < newsvector.size() ;j++) {file<<newsvector[j];} file.close(); } else cout<<"error creating news archive"; return; } void sendfile(const char*msg) { // implemented later// } int main (int argc, char*argv[]) /*main program accepting port number , else arguments */ { int sockfd, newsockfd, portno, clilen, n,test; string publish="publish"; string search= "search"; string get="get"; string newsstring = "news"; string docstring = "doc"; string photostring = "photo"; string wrap = "exit"; char buffer[256]; char seats [50]; serverlogs("server running"); struct sockaddr_in serv_addr, cli_addr; /* defines server address , client address , types of structures same format socket address */ if (argc < 2) { fprintf(stderr,"please provide port number next time plz, goodbye!"); exit(1); } sockfd = socket(af_inet, sock_stream, 0); /* if sock_dgram then open udp socket */ if (sockfd < 0) error("error opening socket"); bzero((char *)&serv_addr, sizeof(serv_addr)); /* sets server address 0 usings reference */ portno = atoi(argv[1]); /*2nd argument vector port number , converted integer, first [0] running program */ serv_addr.sin_family = af_inet; /* structure sockaddr_in has 4 fields, first 1 , should */ serv_addr.sin_port = htons(portno); /* convert port number network byte order */ serv_addr.sin_addr.s_addr = inaddr_any; /*3rd field uses ip address of host machine */ if (bind(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr))<0) /*bind socket sockfd , address of server ,and needs size of address need struct of sockaddr, see bind man details*/ error("couldn't bind socket address"); listen(sockfd,5); clilen = sizeof(cli_addr); serverlogs("going server loop"); int pid = fork(); if (pid < 0) serverlogs("error on initial separtion of server , client"); if (pid==0) { // server loop later on child process , client main while (1) { /* server loop loop*/ newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, (socklen_t*)&clilen); if (newsockfd < 0) /* error("corrupt log"); */ serverlogs("connection not accepted"); else (newsockfd > 0); int ppid = fork(); if (ppid < 0) serverlogs("a new server process failed created"); if (ppid == 0) { int uid; uid = johns; printf("a new client connected identity: %d\n", getpid()); close(sockfd); serverlogs("a john connected server"); printf("my unique id: %d\n", uid); serverlogs(seats); bzero(buffer,256); //n = write(newsockfd,"each client 'john' \n",26); /* loop read socket , writes other persons sockets */ while(1) { bzero(buffer,256); n = read(newsockfd,buffer,256); if (n < 0) printf ("error reading socket here message: %s",buffer); if (publish[0] == buffer[0]) { n = write(newsockfd,"what category publishing in?(news,photos,documents) \n",62); bzero(buffer,256); n = read(newsockfd,buffer,256); if(buffer[0]==newsstring[0]) { n = write(newsockfd,"type name of file publish , wait 10 seconds \n",59); bzero(buffer,256); n = read(newsockfd,buffer,256); publishnewsvector(buffer); serverlogs(seats); serverlogs("client trying publish news file:"); serverlogs(buffer); /*start recieving file*/ std::ofstream file; file.open(buffer, std::ios::out | std::ios::binary); assert(file.is_open()); while (1) { std::cout << ".."; bzero(buffer,256); n = read(newsockfd, buffer, sizeof(buffer)); assert(n != -1); if (n == 0) break; file.write(buffer, n); i++; } file.close(); serverlogs("file transfered successfully"); } if(buffer[0]==docstring[0]) { n = write(newsockfd,"type name of file publish \n",39); bzero(buffer,256); n = read(newsockfd,buffer,256); publishdocsvector(buffer); serverlogs(seats); serverlogs("client tyring publish document:" ); serverlogs(buffer); /*start recieving file*/ std::ofstream file; file.open(buffer, std::ios::out | std::ios::binary); assert(file.is_open()); while (1) { std::cout << "."; bzero(buffer,256); n = read(newsockfd, buffer, sizeof(buffer)); assert(n != -1); if (n == 0) break; file.write(buffer, n); } file.close(); serverlogs("file transfered successfully"); } if(buffer[0]==photostring[0]) { n = write(newsockfd,"type name of file publish \n",39); bzero(buffer,256); n = read(newsockfd,buffer,256); publishphotosvector(buffer); serverlogs(seats); serverlogs("client trying publish photo file:" ); serverlogs(buffer); /*start recieving file*/ std::ofstream file; file.open(buffer, std::ios::out | std::ios::binary); assert(file.is_open()); while (1) { std::cout << "."; bzero(buffer,256); n = read(newsockfd, buffer, sizeof(buffer)); assert(n != -1); if (n == 0) break; file.write(buffer, n); } file.close(); serverlogs("file transfered successfully"); } } if (get[0] ==buffer[0]) { n = write(newsockfd,"\n file want get? \n",35); bzero(buffer,256); n = read(newsockfd,buffer,256); serverlogs(seats); serverlogs("client wants file:"); serverlogs(buffer); //** start sending file**// file* searchfile = 0; long size = 0; searchfile = fopen(buffer,"r"); fseek(searchfile, 0, seek_end); size = ftell(searchfile); char *bufferfile = (char*)malloc(size); memset(bufferfile, 0, size); fseek(searchfile, 0, seek_set); fread(bufferfile, size, 1, searchfile); n = write(newsockfd,bufferfile,256); fclose(searchfile); serverlogs("sent file client"); serverlogs(seats); } if (search[0] == buffer[0]) { bzero(buffer,256); n = write(newsockfd,"what category searching? \n",35); bzero(buffer,256); n = read(newsockfd,buffer,256); serverlogs(seats); serverlogs("client searching for"); serverlogs(buffer); if(buffer[0]==newsstring[0]) { file* searchfile = 0; long size = 0; searchfile = fopen("newsfiles.txt","r"); fseek(searchfile, 0, seek_end); size = ftell(searchfile); char *bufferfile = (char*)malloc(size); memset(bufferfile, 0, size); fseek(searchfile, 0, seek_set); fread(bufferfile, size, 1, searchfile); n = write(newsockfd,bufferfile,256); fclose(searchfile); } if(buffer[0]==docstring[0]) { file* searchfile = 0; long size = 0; searchfile = fopen("docfiles.txt","r"); fseek(searchfile, 0, seek_end); size = ftell(searchfile); char *bufferfile = (char*)malloc(size); memset(bufferfile, 0, size); fseek(searchfile, 0, seek_set); fread(bufferfile, size, 1, searchfile); n = write(newsockfd,bufferfile,256); fclose(searchfile); } if(buffer[0]==photostring[0]) { file* searchfile = 0; long size = 0; searchfile = fopen("photofiles.txt","r"); fseek(searchfile, 0, seek_end); size = ftell(searchfile); char *bufferfile = (char*)malloc(size); memset(bufferfile, 0, size); fseek(searchfile, 0, seek_set); fread(bufferfile, size, 1, searchfile); n = write(newsockfd,bufferfile,256); fclose(searchfile); } } if(buffer[0]==wrap[0]) { close(sockfd); close(newsockfd); johns = johns - 1; serverlogs("a john left"); return 0; } n = write(newsockfd,"\n waiting command can understand:(publish,search,get,exit)",66); } /* while loop listen commands client bracket */ }/*what when client connected*/ } /*creating child/zombies */ } /* division between client , server*/ else { while (1) {int ssockfd, pportno, p,peer; struct sockaddr_in peer_addr; struct hostent *peerserver; char bbuffer[256]; printf ("%s \n", "welcome social"); printf ("%s \n", "please type port number of server \n"); cin>>pportno; ssockfd = socket(af_inet, sock_stream, 0); if (sockfd < 0) error("error opening socket please try again"); printf ("%s \n", "please type in hostname of client ie sunlab1.njit.edu \n"); char peers[256]; cin >> peers; peerserver = gethostbyname(peers); if (peerserver == null) { fprintf(stderr,"could not connect ip address"); peerserver = gethostbyname((const char*)peer); if (peerserver == null) fprintf(stderr,"still null"); exit(0); } bzero((char *) &peer_addr, sizeof(peer_addr)); peer_addr.sin_family = af_inet; bcopy((char *)peerserver->h_addr,(char *)&peer_addr.sin_addr.s_addr,peerserver->h_length); peer_addr.sin_port = htons(pportno); if (connect(ssockfd,(struct sockaddr *)&peer_addr,sizeof(peer_addr)) < 0) error("error connecting"); clientlogs("connected peer"); clientlogs((const char*)&peer); printf("please enter command, publish,search or get: "); while (1) { bzero(bbuffer,256); fgets(bbuffer,255,stdin); char apub[] = "publish"; char asearch[] = "search"; char aget[]="get"; if (bbuffer[0]==apub[0] && bbuffer[1]==apub[1]) { p = write(ssockfd,bbuffer,strlen(bbuffer)); if (p < 0) error("error writing socket"); bzero(bbuffer,256); p = read(ssockfd,bbuffer,255); if (p < 0) error("error reading socket"); printf("%s",bbuffer); //what category publishing in// bzero(bbuffer,256); fgets(bbuffer,255,stdin); p = write(ssockfd,bbuffer,strlen(bbuffer)); clientlogs("client publishing"); clientlogs(bbuffer); if (p < 0) error("error writing socket"); bzero(bbuffer,256); p = read(ssockfd,bbuffer,255); if (p < 0) error("error reading socket"); printf("%s",bbuffer); //type name of file// fgets(bbuffer,255,stdin); p = write(ssockfd,bbuffer,strlen(bbuffer)); clientlogs(bbuffer); file* searchfile = 0; long size = 0; searchfile = fopen(bbuffer,"r"); fseek(searchfile, 0, seek_end); size = ftell(searchfile); char *bufferfile = (char*)malloc(size); memset(bufferfile, 0, size); fseek(searchfile, 0, seek_set); fread(bufferfile, size, 1, searchfile); p = write(ssockfd,bufferfile,256); fclose(searchfile); } if(bbuffer[0]==aget[0] && bbuffer[1]==aget[1]) { p = write(ssockfd,bbuffer,strlen(bbuffer)); if (p < 0) error("error writing socket"); bzero(bbuffer,256); p = read(ssockfd,bbuffer,255); if (p < 0) error("error reading socket"); printf("%s",bbuffer); //what file want get? // bzero(bbuffer,256); fgets(bbuffer,255,stdin); p = write(ssockfd,bbuffer,strlen(bbuffer)); if (p < 0) error("error writing socket"); clientlogs("client wants file:"); clientlogs(bbuffer); /*start recieving file*/ std::ofstream file; file.open(bbuffer, std::ios::out | std::ios::binary); assert(file.is_open()); char buffer[255]; while (1) { std::cout << "."; bzero(bbuffer,256); p = read(ssockfd, bbuffer, sizeof(bbuffer)); assert(p != -1); if (p == 0) break; file.write(bbuffer, p); } file.close(); serverlogs("file transfered successfully"); } if (bbuffer[0]==asearch[0] && bbuffer[1]==asearch[1]) { p = write(ssockfd,bbuffer,strlen(bbuffer)); if (p < 0) error("error writing socket"); bzero(bbuffer,256); p = read(ssockfd,bbuffer,255); if (p < 0) error("error reading socket"); printf("%s",bbuffer); // category? // fgets(bbuffer,255,stdin); p = write(ssockfd,bbuffer,strlen(bbuffer)); if (p < 0) error("error writing socket"); clientlogs("client searching file"); clientlogs(bbuffer); bzero(bbuffer,256); p = read(ssockfd,bbuffer,255); if (p < 0) error("error reading socket"); printf("%s",bbuffer); //the vector array of files // } if (bbuffer[0]==wrap[0] && bbuffer[1]==wrap[1]) { p = write(ssockfd,bbuffer,strlen(bbuffer)); exit(0); } }//end of client loop asking enter command } } return 0;
}
the read()
function (from <stdio.h>
) takes file descriptor, not ifstream
object. should use ifstream
's read()
method instead:
files.read(filebuffer, sizeof(filebuffer)); int bytes_read = files.gcount(); if(files.eofbit && !bytes_read) break; if(files.badbit) error("error reading file");
Comments
Post a Comment