7 - 4 HTTPServer
7 - 4 HTTPServer
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
HTTP Server Using Socket
Programming
Step 3: Bind server_fd and address
} //end while(1)
• Note that “new_socket “ will be used for further communication
HTTP Server Using Socket Programming
Step 6: Read and Print data received from client
while(1)
{
if ((new_socket = accept(server_fd,………
……………………………………………………………..
} //end while(1)
HTTP Server Using Socket Programming
Step 7: Create HTTP Response in a String
char *server_response = "HTTP/1.1 200 OK\nContent-Type:
text/html\nContent-Length: 72\n\n <!DOCTYPE
HTML><HTML><body><h1>Hello Thapar
Institute</h1></body></HTML>";
while(1)
{
…………………..
}
• Here, we are sending HTTP response header appended
with HTML content <html><body><h1>Hello Thapar
Institute</h2></body></html>
HTTP Server Using Socket Programming
Step 7: Send the Response to client and close new_socket
char *server_response = "HTTP/1.1 200 OK\nContent-Type:
text/html\nContent-Length: 72\n\n <!DOCTYPE
HTML><HTML><body><h1>Hello Thapar Institute
</h1></body></HTML>";
while(1)
{
………accept()…………..
…….print client data….
write(new_socket , server_response , strlen(server_response));
printf("-------Response sent to client---------\n");
close(new_socket);
}//end while(1)