0% found this document useful (0 votes)
79 views3 pages

Re

This C code defines a web server that listens on port 8080 for client connections. When a client connects, the server forks a child process to handle the request. The child process reads the request, checks if it is for a favicon or image file, and if so sends that file to the client. Otherwise, it sends a simple HTML page as a response. Includes header files define sockets and networking functionality used to implement the server.

Uploaded by

Syahid Farhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views3 pages

Re

This C code defines a web server that listens on port 8080 for client connections. When a client connects, the server forks a child process to handle the request. The child process reads the request, checks if it is for a favicon or image file, and if so sends that file to the client. Otherwise, it sends a simple HTML page as a response. Includes header files define sockets and networking functionality used to implement the server.

Uploaded by

Syahid Farhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

char webpage[] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html>\r\n"
"<html><head><title>ShellwaveX</title>\r\n"
"<style>body { background-color: #FFF00 }</style></head>\r\n"
"<body><center><h1>Hello world! </h1><br>\r\n"
"<img src=\"doctest.jpg\"></center></body></html>\r\n" ;

int main (int argc, char *argv[])


{
struct sockaddr_in server_addr, client_addr;
socklen_t sin_len = sizeof(client_addr);
int fd_server, fd_client;
char buf[2048];
int fdimg;
int on = 1;

fd_server = socket(AF_INET, SOCK_STREAM, 0);


if (fd_server < 0)
{
perror ("socket");
exit(1);
}

setsockopt(fd_server, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);

if(bind(fd_server, (struct sockaddr *) &server_addr, sizeof(server_addr)) ==


-1)
{
perror("bind");
close(fd_server);
exit(1);
}

if (listen(fd_server, 10) == -1)


{
perror("listen");
close(fd_server);
exit(1);
}

while(1)
{
fd_client = accept(fd_server, (struct sockaddr *) &client_addr,
&sin_len);

if(fd_client == -1)
{
perror("Connection failed...\n");
continue;
}

printf("Got client connection.....\n");

if(!fork())
{
/* child process */
close(fd_server);
memset(buf, 0, 2048);
read(fd_client, buf, 2047);

printf("&s\n", buf);

if (!strncmp(buf, "GET /favicon.ico", 16))


{
fdimg = open("favicon.ico", O_RDONLY);
sendfile(fd_client, fdimg, NULL, 4000);
close(fdimg);
}

else if (!strncmp(buf, "GET /doctest.jpg", 16))


{
fdimg = open("doctest.jpg", O_RDONLY);
sendfile(fd_client, fdimg, NULL, 4000);
close(fdimg);
}
else
write(fd_client, webpage, sizeof(webpage) - 1);

close (fd_client);
printf("closing...\n");
exit(0);
}

/* parent process */
close(fd_client);
}

You might also like