Thread: Send and Int Through Socket?

  1. #1
    Registered User
    Join Date
    Apr 2006

    Send and Int Through Socket?

    Is it possible to send an int throug a udp or tcp socket program without casting it? I have an assignment and the requirments state "It is not acceptable to translate integers into a string for transmitting them over the network". I thought you could only send strings are items that have been casted to string? Am I incorrect and you can send ints without casting?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Katy, Texas
    Try htonl().

    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    In the final end what gets transmitted through a socket, thanks to the many layers of
    abstraction provided by the network protocols, is raw bytes.
    Whether the receiver chooses to interpret them as strings, or ints, or floats or whatever
    data structure the receiver deems necessary is the receivers responsibility.
    So yes, just send the int using htonl to place the bytes in Network Byte Order, and
    use the correct size and address for the send, and you are finished on the sender's side.
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of programming practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  4. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  5. code problem
    By kashifk in forum C Programming
    Replies: 6
    Last Post: 04-22-2003, 05:22 PM