Thread: parse doubles from socket read?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    parse doubles from socket read?

    Hi,

    I am going to work w/ a friends app and he has a socket that will send about 10 doubles so I need to connect to it and read these doubles.

    I know how to make the socket, but I'm not sure of how to properly read the doubles or pull them out. So far I have below ... can anyone help me here.

    Code:
    #define BUF_SZ 64
    
    int retval;
    char buffer[BUF_SZ];
    
    memset(buffer, 0x0, BUF_SZ);
    retval = read(fd, buffer, BUF_SZ);

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    Unfortunately, this is a trickier question than maybe it seems at first glance. If I understand correctly, there is no "standard" way to send a float or double typed value across the wire. You'll have to know more details about the "protocol" the sender is going to follow. How are they going to format that message? Maybe they convert the double to a string and send it out over the wire to a predetermined precision? Then each double maybe is delimited by a space character?

    Once you know the details like this, you can probably parse the string read into buf with the sscanf function.

    Take a look at this, I thought it was a wonderful reference when I first started playing with network programming:
    http://www.beej.us/guide/bgnet/outpu...age/index.html

    Section 6.4 discusses among other things, methods of sending floats (or doubles) across the network.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    2
    Here's what I do know. He said he put the doubles in as just doubles, no delimiters, not as strings or anything else. Just doubles and I should get them by their size which I think is 64 bits.

    How do I grab them that way? I didn't find it that way in that link.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    double buffer[10] = {0};
    
    retval = read(fd, buffer, sizeof buffer);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    87
    I think you should read up on the read() function. It's return value and third argument are both related to the number of bytes in the message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot read incoming data from socket
    By fnoyan in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2006, 02:42 AM
  2. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  3. Socket class OOD
    By lord mazdak in forum C++ Programming
    Replies: 6
    Last Post: 12-17-2005, 01:11 AM
  4. problem closing a socket
    By Wisefool in forum C Programming
    Replies: 1
    Last Post: 10-28-2003, 01:38 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM