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.
>
|