Send and Int Through Socket?

This is a discussion on Send and Int Through Socket? within the C Programming forums, part of the General Programming Boards category; Is it possible to send an int throug a udp or tcp socket program without casting it? I have an ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    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
    Location
    Katy, Texas
    Posts
    2,309
    Try htonl().

    Todd
    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
    Location
    Pacem
    Posts
    355
    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.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap 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, 10:19 AM
  2. Replies: 8
    Last Post: 03-10-2008, 12:57 PM
  3. Replies: 14
    Last Post: 06-28-2006, 02: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, 06:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21