Thread: Determining Latency of Web Sites

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    Determining Latency of Web Sites

    I am writing a simple program that connects a socket to, say google, and then waits for a response. Basically, I am determining the latency of web sites over a 24 hour period. I am doing something wrong because my writes aren't working. I use port 80 and copy GET / http/1.0\r\n\r\n into a buffer and write it to google. My program seems to be sticking in the write process, and I have no ideas if the read will work or not. I am using AF_INET and SOCK_STREAM to create my socket file descriptors. Any ideas?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Need to see some code. Also, read the FAQ thread in the Linux forum for various info.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Here is my code:

    Code:
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    
    void error(char *msg)
    {
        perror(msg);
        exit(0);
    }
    
    int main(int argc, char *argv[])
    {
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
    
        char buffer[256];
        if (argc < 2) {
           fprintf(stderr,"usage %s hostname\n", argv[0]);
           exit(0);
        }
        portno = 80;
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
        bzero((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(portno);
        if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
            error("ERROR connecting");
        strcpy(buffer, "GET / http/1.0\r\n\r\n");
        bzero(buffer,256);
        fgets(buffer,255,stdin);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)
             error("ERROR writing to socket");
        printf("wrote to site");
        bzero(buffer,256);
        n = read(sockfd,buffer,255);
        if (n < 0)
             error("ERROR reading from socket");
        printf("%s\n",buffer);
        return 0;
    }
    All I need is for google to get my request and send back one line. I am determining the latency over a 24 hour period.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Start here:

    strcpy(buffer, "GET / http/1.0\r\n\r\n"); // copy.
    bzero(buffer,256); // erase?
    fgets(buffer,255,stdin); // fill?!!

    That won't work, now will it?

    Drop the last two statements and try again.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    12
    Thank you! Next time I will check my code a little closer before posting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. interact with web sites
    By awnjoor in forum C Programming
    Replies: 8
    Last Post: 07-26-2006, 07:10 PM
  2. Determining values on a web page
    By AaA in forum C Programming
    Replies: 1
    Last Post: 06-28-2005, 04:47 AM
  3. Getting the ip address of web sites?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-18-2002, 12:16 AM
  4. Game Company Web Sites
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-17-2001, 08:32 PM