The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Computer Networks-Socket Programming
Create simple httpclient.c and httpserver.c. Here is the code I have written so far.
For client.c, I need to change the code so it can
1. take and submit a GET request;
2. be able to accept parameter "option" and if the option is "-p", calculate and return RTTÂ
It should be able to take following command lines
./client www.google.com 80(some port number)
./client web.mit.edu/index.html 'some port number'
./client -p www.cnn.com 80
Â
Â
Â
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <netdb.h>#include <netinet/in.h>#include <string.h>#define MAXBUFFER 500int main(int argc, char *argv[]) {struct sockaddr_in serv_addr;struct hostent *server = gethostbyname(argv[1]);int sock, n;int port_number = atoi(argv[2]);char buffer[MAXBUFFER];//Check if there are right number of argumentsif (argc < 3) {fprintf(stderr,"usage %s hostname port\n", argv[0]);exit(0);}//Create a stream socketsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);if (sock < 0) {perror("ERROR: socket() failed\n");exit(1);}if (server == NULL) {fprintf(stderr,"ERROR: no such host\n");exit(0);}//Fill in the server address structurebzero((char *) &serv_addr, sizeof(serv_addr));serv_addr.sin_family = AF_INET;bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);serv_addr.sin_port = htons(port_number);// Connect to the serverif (connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {perror("ERROR: connect() failed\n");exit(1);}//Give the Get requestprintf("Please enter the request: ");bzero(buffer,MAXBUFFER);fgets(buffer,MAXBUFFER - 1,stdin);//Send requestn = write(sock, buffer, strlen(buffer));if (n < 0) {perror("ERROR: write() failed");exit(1);}//Read server responsebzero(buffer,MAXBUFFER);