Thread: socket and time

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    32

    socket and time

    Hi,

    I'm working on a small school assignment where I should make my program on a Solaris that connects to a server with TCP and gets a 32-bit integer that contains the time which should then be displayed on the command line.

    Now, I don't know much unix and although I've done some programming in Java this is my first program in C.

    I think I've connected to the server and downloaded the 4 bytes, but now I don't know how to make them display correctly.

    Here are my questions:

    1) I wish to see the value of the 4 bytes I downloaded. I've made an attempt in the code. But it doesn't look like any of the examples I've got; 2208988800 should correspond to 00:00 1 jan 1970 GMT and -1297728000 should correspond to 00:00 17 nov 1858 GMT. I get -63.

    2) I have no idea about the timeformat of these 4 bytes. I think the time format is standardized but I don't know the specification or where to look for it, or how to convert it, so I'd like to know where I should look for this.

    3) Why doesn't my casting work on the last line of code? I'd like to print it as an unsigned int, but still get the signed -63.

    Seron

    Below is my code
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <time.h>
    
    #define SERVER_PORT 37
    
    int main() {
        
        char *time;
        int sock, byte_count;
        struct sockaddr_in sin;
        struct hostent *hp;
        char *host;
    
        host = "roman.ludat.lth.se";
        hp = gethostbyname(host);
        if(!hp) {
    	printf("unknown host: %s\n", host);
    	exit(-1);
        }
    
        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
        sin.sin_port = htons(SERVER_PORT);
    
        if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    	perror("socket");
    	exit(-1);
        }
    
        if(connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    	perror("connect");
    	exit(-1);
        }
        
        byte_count = recv(sock, time, 4, 0);
        printf("*time = %d\n", (unsigned int)*time);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char *time;
    ...
    byte_count = recv(sock, time, 4, 0);
    The 'time' pointer doesn't have any space allocated for it. The second argument for 'recv' is to a buffer (ie: some valid space in which it can store something). You are giving it some random uninitialized space.

    Either use an array for 'time', or allocate space for it.

    Furthermore, you'll probably want to make this a single line:
    char *host;

    host = "roman.ludat.lth.se";
    char *host = "roman.ludat.lth.se";


    [edit]Curses! Foiled again![/edit]



    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thanks for the quick replies.

    I've got the right format now for the time, but it is not showing the correct time, unless that is what the server sends me.

    more questions:

    1) How could I check that what I have is the right sequence of bits? I don't know what type of machine is at the other end.

    2) Am I still using pointers incorrectly?

    3) If I put unsigned long int time2 = 0; lower down in the code, for example where the /**/ is the compiler (gcc) complains with a parse error. Why?

    Seron

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <time.h>
    
    #define SERVER_PORT 37
    
    int main() {
        
        time_t time = 0;
        int sock;
        struct sockaddr_in sin;
        struct hostent *hp;
        char *host = "roman";
        char time_string[27];
        char *tsp = time_string;
        unsigned long int time2 = 0;
    
        time_string[26] = '\0';
    
        // for debugging
        printf("time_string = %d\n", time_string);
        printf("time = %d\n", time);
        printf("host = %d\n", host);
    
        hp = gethostbyname(host);
        if(!hp) {
    	printf("unknown host: %s\n", host);
    	exit(-1);
        }
    
        bzero((char *)&sin, sizeof(sin));
        sin.sin_family = AF_INET;
        bcopy(hp->h_addr, (char *)&sin.sin_addr, hp->h_length);
        sin.sin_port = htons(SERVER_PORT);
    
        if((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    	perror("socket");
    	exit(-1);
        }
    
        if(connect(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    	perror("connect");
    	exit(-1);
        }
        
        recv(sock, &time, 4, 0); // put some errorchecking here
        tsp = ctime(&time);
    
    /**/
    
        // for debugging
        printf("time = %d\n", time);
        memmove( &time2, &time, sizeof(time2) );
        printf("time = %u\n", time2 );
    
        // prints the time
        printf("%s\n", tsp);
        tsp = ctime(&time2);
        printf("%s\n", tsp);
    }
    output:

    Code:
    time_string = -4261488
    time = 0
    host = 68248
    time = -1049019304
    time = 3245947992
    Sun Oct  4 14:44:56 1936
    
    Sun Oct  4 14:44:56 1936

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    32
    Thanks for your help. I still get a strange date and time. This part of the code now looks like this:
    Code:
    recv(sock, &time, 4, 0);
    time = ntohl(time);
    tsp = ctime(&time);
    
    printf("%s\n", tsp);
    I'm going to sleep over it now.

    Seron

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get ping time in miliseconds from the socket?
    By Anddos in forum Networking/Device Communication
    Replies: 1
    Last Post: 06-26-2009, 10:27 AM
  2. how to block a socket indefinitely
    By radeberger in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-18-2009, 08:39 AM
  3. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  4. Socket Help - Multiple Clients
    By project95talon in forum C Programming
    Replies: 5
    Last Post: 11-17-2005, 02:51 AM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM