hi i am trying to write a simple web server which when i go to my browser and type in localhost:5000 the web server serves a html file
heres my code so far i have 2 versions of code the first one has a segmentation fault which i cant figure out and the second version just trys to download a binary file or view it in the browser.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<strings.h>
#include<string.h>
#include<unistd.h>
#include<signal.h>
#include<errno.h>
#define DEFAULT_PROTOCOL 0
#define MAXBYTE 4096
#define PATH_SIZE 128
int main(int argc, char *argv[])
{
int listenfd, connectfd, port, val;
char recive[MAXBYTE];
int client_length;
char buffer[256];
char buff[255] = {0};
int n;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
signal(SIGCHLD, SIG_IGN);
if (argc < 2) {
fprintf(stderr,"ERROR, no port enterd\n");
exit(1);
}
if((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
{
perror("SOCKET");
exit(EXIT_FAILURE);
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bzero((char *) &serveraddr, sizeof(serveraddr));
port = atoi(argv[1]);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(port);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if((bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
listen(listenfd,10);
client_length = sizeof(clientaddr);
connectfd = accept(listenfd, (struct sockaddr *)&clientaddr, &client_length);
if (connectfd < 0)
error("eRROR on accept");
bzero(buffer,256);
n = read(connectfd, buffer, 255 );
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
FILE *open = NULL;
char err_msg[] = "Could not open";
if(open = fopen("/home/rocco/www/home.html", "r"))
{
n = write(connectfd, err_msg, strlen(err_msg));
n = write(connectfd, "/home/rocco/www/home.html", strlen("/home/rocco/www/home.html"));
n = recv(connectfd, buff, 255, 0);
fprintf (open, "%s", buff);
exit(EXIT_FAILURE);
}
while ((write(connectfd, recive, fread(recive, sizeof(char), MAXBYTE, open))))
{}
fclose(open);
close(connectfd);
exit(EXIT_SUCCESS);
}
2nd version
Code:
#include<stdio.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<strings.h>
#include<string.h>
#include<unistd.h>
#include<signal.h>
#include<errno.h>
#define DEFAULT_PROTOCOL 0
#define MAXBYTE 4096
#define PATH_SIZE 128
int main(int argc, char *argv[])
{
int listenfd, connectfd, port, val;
char recive[MAXBYTE];
int client_length;
char buffer[256];
char buff[255] = {0};
int n;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
signal(SIGCHLD, SIG_IGN);
if (argc < 2) {
fprintf(stderr,"ERROR, no port enterd\n");
exit(1);
}
if((listenfd = socket(AF_INET, SOCK_STREAM, DEFAULT_PROTOCOL)) < 0)
{
perror("SOCKET");
exit(EXIT_FAILURE);
}
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bzero((char *) &serveraddr, sizeof(serveraddr));
port = atoi(argv[1]);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(port);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if((bind(listenfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) < 0)
{
perror("bind");
exit(EXIT_FAILURE);
}
listen(listenfd,10);
client_length = sizeof(clientaddr);
connectfd = accept(listenfd, (struct sockaddr *)&clientaddr, &client_length);
if (connectfd < 0)
error("eRROR on accept");
bzero(buffer,256);
n = read(connectfd, buffer, 255 );
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s\n",buffer);
FILE *open = NULL;
char err_msg[] = "Could not open";
while((open = fopen("/home/rocco/ww/home.html","r") != EOF))
{
n = write(connectfd, &open,1);
}