Networking Forums

Networking Forums > Computer Networking > Linux Networking > sendto() and structures

Reply
Thread Tools Display Modes

sendto() and structures

 
 
ericunfuk
Guest
Posts: n/a

 
      03-24-2007, 11:54 AM
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
 
 
 
 
Robert Harris
Guest
Posts: n/a

 
      03-24-2007, 12:22 PM
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
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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating neighbor solicitation packet with c language and ancillarydata structures pdbuchan@yahoo.com Linux Networking 7 11-26-2011 03:14 PM
Using vs. Avoiding Steel Structures jlaham Wireless Internet 1 10-15-2007 10:15 AM
Is count of un acknowldged bytes maitained anywhere in kernel data structures for TCP socket ? Rohit Linux Networking 0 05-14-2007 12:23 PM
max iovec structures in msghdr? Oliver Kowalke Linux Networking 0 01-15-2007 05:16 PM
Help with sendto() B.r.K.o.N.j.A. Linux Networking 0 12-11-2003 09:07 PM



1 2 3 4 5 6 7 8 9 10 11