sendto() and structures

This is a discussion on sendto() and structures within the Linux Networking forums, part of the Linux Forums category; Can I send a structure using sendto()? I'm doing something like the following: n = sendto(sock,&packet,sizeof(...


Go Back   Usenet Forums > Linux Forums > Linux Networking

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-24-2007
ericunfuk
 
Posts: n/a
Default sendto() and structures

Can I send a structure using sendto()? I'm doing something like the
following:

n = sendto(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
*)&gateway,sizeof(struct sockaddr_in));


where packet is: struct dgram packet;

struct dgram {

unsigned long client_seq_no;
unsigned long win_size;
unsigned short flag;
unsigned char data[5];

};


Will I be able to use recvfrom to recover the "packet" in the receiver
end?like:

n = recvfrom(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
*)&sender,&senderlen);



Any help appreciated.

Reply With Quote
  #2 (permalink)  
Old 03-24-2007
Robert Harris
 
Posts: n/a
Default Re: sendto() and structures

ericunfuk wrote:
> Can I send a structure using sendto()? I'm doing something like the
> following:
>
> n = sendto(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
> *)&gateway,sizeof(struct sockaddr_in));
>
>
> where packet is: struct dgram packet;
>
> struct dgram {
>
> unsigned long client_seq_no;
> unsigned long win_size;
> unsigned short flag;
> unsigned char data[5];
>
> };
>
>
> Will I be able to use recvfrom to recover the "packet" in the receiver
> end?like:
>
> n = recvfrom(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
> *)&sender,&senderlen);


You can send whatever you like (up to a maximum packet length) and what
you receive will be the same as what you send. But if you send on one
machine and receive on another, be careful that:

1. the field sizes are the same on both machines (e.g. unsigned long has
the same length on each). unsigned longs are normally 32 bits long on a
traditional PC but 64 bits long on an AMD64.
2. the endianness is the same on both machines (i.e. integers are stored
in the same byte order)
3. Structure padding is the same on both machines (which in your example
it almost certainly will be).

You can overcome 1. by using fixed size data types (e.g. uint32_t). You
can overcome 2. by standardising on a network order, e.g. by using
ntohl() and friends.

If you send your integers in ASCII, the above problems disappear.

Robert
>
>
>
> Any help appreciated.
>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:01 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0