Thread: Converting a struct members to a string, c newbie!

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    1

    Converting a struct members to a string, c newbie!

    Hi there.

    I am trying to make a client for use with an sntp server.

    The idea is that I send a datagram via the udp socket system to the server containing the current computer time and it responds with the accurate time which I can then compare and update the clock as necessary.

    So, I have a struct that creates bitfields in order to build the required packet:

    Code:
    struct {
    int mode:3;
    long long timestamp:64;
    } message
    Obviously there are many more fields.

    Now I want to send that using the usual socket send command. However, the send command takes a string as the thing to be sent.

    So how do I take each member of the struct, convert it to a string and build up the total string that I need to send, ie put all of the struct memebrs into one long string?

    I am new to C and finding it very difficult so any help would be much appreciated!

    Thanks in advance

    Daniel

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>However, the send command takes a string as the thing to be sent.<<
    Are you talking about the send() function? If so, you can do this if you want to send the struct itself:

    send(socket, &mystruct, sizeof(mystruct), 0);

    But its going to depend on what the server is expecting to receive.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM