I am trying to read the contents from the specified file into a char array and then send that to the client. The problem i get is that getline() wont take a char array as a parameter and strcat() wont take a string as a parameter so I dont know what to do. Are there other functions i could use? I dont know of any so any help will be VERY appreciated.
Lines in question are 78 and 80
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int parseARGS(char **args, char *line){
int tmp=0;
args[tmp] = strtok( line, " /" );
while ( (args[++tmp] = strtok(NULL, " ,/" ) ) != NULL );
return tmp - 1;
}
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portNum, clilen;
int nbytes=0;
char *line;
char *header[4096];
char *filename;
char buffer[1024];
char filebuffer[4096];
char strfinal[4096];
struct sockaddr_in serv_addr, cli_addr;
int n, percent, count1, count2;
int received = 0;
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
portNum = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portNum);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
while(1){
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, (socklen_t*)&clilen);
if (newsockfd < 0)
error("ERROR on accept");
n = read(newsockfd,buffer,1023);
parseARGS(header, buffer);
filename = header[1];
printf("Here is the message: %s\n",filename);
if (n < 0) error("ERROR reading from socket");
ifstream myfile (filename);
strcat(strfinal, "HTTP/1.0 200 OK \r\n\r\n");
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line);
cout << line << endl;
strcat(strfinal, line);
}
}
send(newsockfd, strfinal, sizeof(strfinal), 0);
if(nbytes < 0) {
perror("sendto");
exit(1);
}
close(newsockfd);
}
return 0;
}