Thread: Sending other datatypes

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  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,895
    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, 09: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, 02:06 PM