is it possible to send a structure using sockets?

This is a discussion on is it possible to send a structure using sockets? within the Networking/Device Communication forums, part of the General Programming Boards category; Asthe title I am trying to make a linux sms system and was hoping I`d be able to send the ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    25

    is it possible to send a structure using sockets?

    Asthe title I am trying to make a linux sms system and was hoping I`d be able to send the data across in a structure. Is this possible or am I going to have to put all the data into a strings?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can send a structure if you want, as long as the receiving end has the same internal representation of the structure (byte alignment etc)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    You can, but all these issues will hurt you at some point.
    - your stucture contains pointers. The socket will not dereference them for you, and they will be meaningless to the receiver. It certainly wouldn't want to dereference it.

    - your structure contains floats. The representation of floats are pretty machine specific, though most seem to use IEEE formats.

    - your structure contains ints. Whether your machine is big endian or little endian (compared to the receiver) affects how ints are interpreted.
    00 00 00 01 may be 1 on your machine, but read in reverse on a different endian machine, it would be 01 00 00 00 (much much bigger than 1)

    Code:
    struct foo { char a; int b; };
    The amount of padding between a and b is implementation specific. Ensuring the padding is the same at both ends can be tricky (and rarely optimal)

    The last 3 you can ignore, IF you're sending to an identical machine running the same code compiled with the same compiler.
    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.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    The code is using sockets to send the data.

    One side will set up a structure which will have an int followed by 3 strings.

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    sendto(sockfd, command, 180, 0,
    (struct sockaddr *)&their_addr, sizeof(struct sockaddr));

    THe structure is called command. its saying that the incompatible type for arg 2 of sendto/

    any ideas?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Read the manual
    &command, sizeof(command)
    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.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    25
    what manual?

    do ya mean the man pages in linux?

    they dont seem to have much details

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > do ya mean the man pages in linux?
    Well that would be a start

    As would reading your C book again.

    I mean, the error message pretty much explains the problem - the types are incompatible.

    So you go read the manual page to find out what type the function really expects, and then work out how to get from the type you have to the type you want.
    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.

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. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  3. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  4. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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