Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Short C program demonstrates a multi-threaded web server. This level of C is beyond me, I don't know how they expect us to know enough

Short C program demonstrates a multi-threaded web server. This level of C is beyond me, I don't know how they expect us to know enough C or C++ when the lectures themselves teach something else.

I know the CreateThread function is needed to be used.

One source file only, 1 main and probably just 1 function.

Build upon the given base code please.

Anyway here is the base code and assignment. Hopefully this will be quick and easy for someone. I know the teacher says it can be done in a few lines. If you can include comments as well I will be so grateful. Thank you!!!!!!!

------------------------------

An incomplete C program is available, the program listens on port 3490 for web page requests and sends a header but does not send the requested page.

#include #include

// Define the port number to identify this process #define MYPORT 3490

int main() { int s,n; unsigned fd; struct sockaddr_in my_addr; WSADATA wsaData; char *header="HTTP/1.1 200 OK Content-Type: text/html "; char data[512]; char filename[256]; FILE *f; // Initialize windows sockets WSAStartup(MAKEWORD(1, 1), &wsaData); // Construct address information my_addr.sin_family = AF_INET; my_addr.sin_port = htons(MYPORT); my_addr.sin_addr.s_addr = INADDR_ANY; memset(my_addr.sin_zero, '\0', sizeof(my_addr.sin_zero) );

// Create a socket and bind it the port MYPORT s=socket(PF_INET,SOCK_STREAM, 0); bind(s, (struct sockaddr *)&my_addr, sizeof(my_addr));

// Allow up to 10 incoming connections listen(s,10);

while(1) { fd=accept(s,NULL,NULL); // wait for a request n=recv(fd,data,512,0); // recieve the request using fd data[n]=0; // NUL terminate it sscanf(data,"GET /%s ",filename); // get the name of the file f=fopen(filename,"rb"); // open the file (might be binary) send(fd,header,strlen(header),0); // send the header // // send the file // closesocket(fd); // close the socket } }

Don't worry about how this works, all you need to use is the send function and C file input (use fread or fgetc, NOT fgets or fscanf)

int send(int s, const void *msg, int len, unsigned int flags); s - a socket (you will use fd) msg - data to send len - number of bytes to send (don't use strlen here if you are sending binary data) flags should be 0

Finish the program so that it sends the requested file from the current directory through the socket- fd. You can test your program using any html or text file. Start a web browser and go to http://127.0.0.1:3490/index.html. If you want, you can add extra error checking to the program to deal with bad requests. Test the program by requesting a large file. While the file is being sent, use a different browser window to make another request, the second request should have to wait for the first one to finish. Rewrite the program using threads so that it can reply to multiple requests at the same time. Do not use any global variables. Submit your source code (the multithreaded version) electronically via stream. Your submission must be a single source file written in ANSI C or C++. Your program should include comments as documentation.

-----------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe barriers to effective listening.

Answered: 1 week ago