Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below is a pretty simple TCP Server. How would I change it in order to make it into an http server that displays a little

Below is a pretty simple TCP Server. How would I change it in order to make it into an http server that displays a little text and picture?

#include #include #include #include #include #include #include #include

// Max message to echo #define MAX_MESSAGE 1000 #define BUF_SIZE 500

/* server main routine */

int main(int argc, char** argv) {

// locals unsigned short port = 22222; // default port int sock; // socket descriptor

// Was help requested? Print usage statement if (argc > 1 && ((!strcmp(argv[1],"-?"))||(!strcmp(argv[1],"-h")))) { printf(" Usage: tcpechoserver [-p port] port is the requested \ port that the server monitors. If no port is provided, the server \ listens on port 22222. "); exit(0); } // get the port from ARGV if (argc > 1 && !strcmp(argv[1],"-p")) { if (sscanf(argv[2],"%hu",&port)!=1) { perror("Error parsing port option"); exit(0); } } // ready to go printf("tcp echo server configuring on port: %d ",port); // for TCP, we want IP protocol domain (PF_INET) // and TCP transport type (SOCK_STREAM) // no alternate protocol - 0, since we have already specified IP if ((sock = socket( PF_INET, SOCK_STREAM, 0 )) < 0) { perror("Error on socket creation"); exit(1); } // lose the pesky "Address already in use" error message int yes = 1;

if (setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) { perror("setsockopt"); exit(1); }

// establish address - this is the server and will // only be listening on the specified port struct sockaddr_in sock_address; // address family is AF_INET // our IP address is INADDR_ANY (any of our IP addresses) // the port number is per default or option above

sock_address.sin_family = AF_INET; sock_address.sin_addr.s_addr = htonl(INADDR_ANY); sock_address.sin_port = htons(port);

// we must now bind the socket descriptor to the address info if (bind(sock, (struct sockaddr *) &sock_address, sizeof(sock_address))<0) { perror("Problem binding"); exit(-1); } // extra step to TCP - listen on the port for a connection // willing to queue 5 connection requests if ( listen(sock, 5) < 0 ) { perror("Error calling listen()"); exit(-1); }

// TODO change filename for jpg you want to send // set up file pointer to the file we want to send FILE *fp = fopen("sad_dog.jpg", "r");

// go into forever loop and echo whatever message is received // to console and back to source char* buffer = calloc(BUF_SIZE,sizeof(char)); int bytes_read; int echoed; int connection; //Set up variables int bytes_from_file; while (1) { // hang in accept and wait for connection printf("====Waiting==== "); if ( (connection = accept(sock, NULL, NULL) ) < 0 ) { perror("Error calling accept"); exit(-1); } // ready to r/w - another loop - it will be broken when // the connection is closed

while(1) { // Send file to the client // read bytes from the file and send bytes_from_file = fread(buffer, sizeof(uint8_t), BUF_SIZE, fp); if ( (echoed = write(connection, buffer, bytes_from_file)) < 0 ) { perror("Error sending echo"); exit(-1); }

if (feof(fp)) { //EOF, need to end the connection printf("====Server Disconnecting==== "); close(connection); break; } } // end of accept inner-while fclose(fp); } // end of outer loop

// will never get here return(0); }

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_2

Step: 3

blur-text-image_3

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

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

=+ Is the information up to date?

Answered: 1 week ago

Question

Describe Balor method and give the chemical reaction.

Answered: 1 week ago

Question

How to prepare washing soda from common salt?

Answered: 1 week ago