Send a numeric

This is a discussion on Send a numeric within the Networking/Device Communication forums, part of the General Programming Boards category; Hi to all, I need to send from server to client a numeric information. If the info is a string, ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    26

    Send a numeric

    Hi to all,

    I need to send from server to client a numeric information. If the info is a string, I write
    send(newsocketfd, MyMsg, sizeof(MyMsg),0);

    If is numeric how can send the value? for example:
    "The number of students are: " 10

    Thank You and Best Regards
    Nick

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Send the string "10" ?
    It's simple to implement, portable, and easy to trace using network debug tools.

    You could do
    send( newsocketfd, &myInt, sizeof(MyInt),0);
    but there's a whole "can-o-worms" of potential problems with that, like what endian the machines use and how big an int is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    typedef's can help with the size issue. Setup a header with some #ifdef's to select the appropriate size on each platform you need to compile for. (There are a couple in stdint.h which is available on many systems - uint8_t, uint16_t, etc.)

    After you've got size accounted for, there are some functions to help with endianness: htons() and htonl(), ntohs(), ntohl(). (Host-to-Network-short/long and Network-to-Host-short/long) So, on the sending size, use htons/l() and on the recv'ing side use ntohs/l(). See the man pages for more info.

    As Salem partially hints, your question is vague as to how you intend to send your data. You can either send 10 in binary (using either 2 or 4 bytes) or you can send the text "10". Which are you trying to accomplish?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tuncated uint8_t
    By Coens in forum C Programming
    Replies: 14
    Last Post: 11-24-2008, 06:57 AM
  2. nonblocking send need help
    By sleith in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-27-2008, 10:51 PM
  3. sending n no of char data uning send() function,
    By thebrighter in forum Windows Programming
    Replies: 1
    Last Post: 08-22-2007, 12:26 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

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