Sending other datatypes

This is a discussion on Sending other datatypes within the Networking/Device Communication forums, part of the General Programming Boards category; Hey! I see that declaration of the send() function is Code: int PASCAL FAR send ( SOCKET s, const char ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    Sending other datatypes

    Hey! I see that declaration of the send() function is
    Code:
    int PASCAL FAR send ( SOCKET s, const char FAR * buf, int len, int flags )
    so I can send only char*s, but what to do if I want to send other variables or datatypes? Example:
    Code:
    struct a
    {
    int x;
    int y;
    string s;
    };
    
    a aObject;
    
    aObject.x=2;
    aObject.y=5;
    aObject.s="gassy";
    Now, how do I send aObject? Also, how to receive it?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > so I can send only char*s,
    No, you can send any contiguous block of data you like. It's just your compiler writer hasn't gotten round to changing it to void*

    > but what to do if I want to send other variables or datatypes?
    Well you can send an int, but you'll have to contend with an endian problem at some point.
    Most people use htonl() and ntohl() to ensure that the result is consistent.

    As for your string, I'm guessing that's an object with way too much system specific information (and no doubt a hidden pointer or two).
    For this, you need to extract the proper string, send that and recreate your string object at the other end.
    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
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Uhh, a lot of fuss, guess I'll have to make one LONG string and send it over. However, thanks for the reply. Maybe others could advise something? Like solutions or different APIs? (if there are any)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Boost.ASIO
    POCO
    ACE

    All of them are C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending data - line by line?
    By tuckker in forum C Programming
    Replies: 0
    Last Post: 02-21-2009, 08:31 PM
  2. signal sending and waiting
    By eva69 in forum Linux Programming
    Replies: 1
    Last Post: 10-03-2008, 05:07 PM
  3. signal sending and waiting
    By eva69 in forum C Programming
    Replies: 1
    Last Post: 10-03-2008, 12:03 PM
  4. Replies: 2
    Last Post: 07-24-2008, 06:05 AM
  5. C++ Datatypes and OS Datatypes :: C++
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 12-07-2002, 01:06 PM

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