Question: Hello everyone, I have a cpp code for a simple webserver. A function call is missing in order to make this code work. Please help.
Hello everyone,
I have a cpp code for a simple webserver. A function call is missing in order to make this code work. Please help. Thank you in advance!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int host_port = 8080;
void usage()
{
printf("myhttpd, a simple webserver ");
printf("ver 1.0, 2014 ");
printf("Usage Summary: myhttpd -h -p portno ");
printf(" -h: display the summary ");
printf(" -p: change default port number for example: -p 8080 ");
}
int httpHandler(int socketfd)
{
char buffer[1024];
int buffer_len = 1024;
int bytecount;
memset(buffer, 0, buffer_len);
if ((bytecount = recv(socketfd, buffer, buffer_len, 0))== -1)
{
fprintf(stderr, "Error receiving data %d ", errno);
return 0;
}
printf("Received bytes %d Received string \"%s\" ", bytecount, buffer);
strcpy(buffer, "HTTP/1.1 200 OK Server: demo Content-Length: 37 Connection: close Content-Type: html
Welcome to my first page!");
if ((bytecount = send(socketfd, buffer, strlen(buffer), 0))== -1)
{
fprintf(stderr, "Error sending data %d ", errno);
return 0;
}
printf("Sent bytes %d ", bytecount);
close(socketfd);
return 0;
}
int main(int argc, char *argv[])
{
struct sockaddr_in my_addr;
int hsock;
int * p_int ;
int err;
int socketfd;
socklen_t addr_size = 0;
int* csock;
sockaddr_in sadr;
pthread_t thread_id=0;
int opt = 0;
opt = getopt( argc, argv,"dhl:p:r:t:n:s:" );
while (opt != -1)
{
switch (opt)
{
case 'h':
usage();
exit(0);
case 'p':
host_port = atoi(optarg);
break;
case 'r':
break;
}
opt = getopt( argc, argv, "dhl:p:r:t:n:s:" );
}
hsock = socket(AF_INET, SOCK_STREAM, 0);
if (hsock == -1)
{
printf("Error initializing socket %d ", errno);
exit(-1);
}
p_int = (int*)malloc(sizeof(int));
*p_int = 1;
if ((setsockopt(hsock, SOL_SOCKET, SO_REUSEADDR, (char*)p_int, sizeof(int)) == -1 )||
(setsockopt(hsock, SOL_SOCKET, SO_KEEPALIVE, (char*)p_int, sizeof(int)) == -1 ) )
{
printf("Error setting options %d ", errno);
free(p_int);
exit(-1);
}
free(p_int);
my_addr.sin_family = AF_INET ;
my_addr.sin_port = htons(host_port);
memset(&(my_addr.sin_zero), 0, 8);
my_addr.sin_addr.s_addr = INADDR_ANY ;
if (bind(hsock, (sockaddr*)&my_addr, sizeof(my_addr)) == -1 )
{
fprintf(stderr,"Error binding to socket, make sure nothing else is listening on this port %d ",errno);
exit(-1);
}
if (listen(hsock, 10) == -1 )
{
fprintf(stderr, "Error listening %d ",errno);
exit(-1);
}
//Now lets do the server stuff
printf("myhttpd server listening on port %d ", host_port);
addr_size = sizeof(sockaddr_in);
while (true)
{
printf("waiting for a connection ");
if ((socketfd = accept( hsock, (sockaddr*)&sadr, &addr_size))!= -1)
{
printf("Received connection from %s ",inet_ntoa(sadr.sin_addr));
}
else
{
fprintf(stderr, "Error accepting %d ", errno);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
