473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

avoid structure padding

pmm
hi I am repeating my post here plz excuse

i am trying out a UDP packet
transfer between a windows machine and a linux I created a structure on
both sides (ie on linux and on windows) and I sent using UDP
but on the receiving side when I am trying to reform the packet back
into structure tis giving me different outputs I assume it is the
problem of structure padding I even used pragma pack (1) but still it
is not working out for me kindly do suggest u r view to execute this
correctly I am palcing structure all well capturing code on recving
side and also print statement of both the machines

kindly suggest me what to modify

my structure
<code>
typedef struct
{
INT32S connId;
INT32S seqNo;

}addrData_t;

typedef struct
{
INT16S typeId;
INT16S length;
addrData_t addrData;

}addrItem_t;

typedef struct
{
INT16S typeId;
INT16S length;
INT8U data[1];

}dataItem_t;

typedef struct
{
INT16S itemCount;
addrItem_t addrItem;
dataItem_t dataItem;

} udpPacket_t;

sending side code lines
INT8U *buff=GetMemBuff(MAX_DATA_LEN);
<code>
udpPacket_t *udpPacket=(udpPacket_t*)buff ;
addrItem_t *ai=&udpPacket->addrItem;
dataItem_t *di=&udpPacket->dataItem;
addrData_t *ad=&ai->addrData;
static int sockFd; int len,thrdId; int siz; int
kkk; extern int
udp_port_no;
extern int connId;
/*static hostent* h;*/
// sockHdl_t *sock = GetNewSockObj();
static sockAddr_t srv;
wVersionRequested = MAKEWORD( 2, 0 );
memset(buff,0,MAX_DATA_LEN);

udpPacket->itemCount=2;

ai->typeId=0x8002;
ai->length=8;
ad->connId=connId;
ad->seqNo=0;
di->typeId=0x00b1;
di->length=1;
di->data[0]=0x12;

siz=sizeof(udpPacket);
printf("\n udpacket size %d\n",siz);

printf("\n typeid %x",ai->typeId);
printf("\nlen %x",ai->length);
printf("\nconn %x",ad->connId);
printf("\nseq %x",ad->seqNo);
printf("\ndtype %x",di->typeId);
printf("\ndlen %x",di->length);
printf("\n data %x",di->data[0]);

srv.sin_family=AF_INET;
sock->sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sock->sfd <0)
{
FreeSockObj(sock);
perror("Unable to get UDP_SOCKET \n");
return FALSE;
}

/* 1.Create socket 2.send data on socket 3 create UDP thrd
4. recv data on Thrd */

if (send(sock->sfd,(const char
*)&udpPacket,sizeof(udpPacket_t),0)
<0)
{
closesocket(sock->sfd);
FreeSockObj(sock);
printf("Unable to send UDP DATA PACKET \n");
return FALSE;
}
else
{
printf("UDP PACKET SENT\n");
}

</code>

this is receving side code

void* IODataRecv(void* sfd)
{
int nsfd=*((int *)sfd);
int len ,i;
char *buff=GetMemBuff(128);

struct in_addr adr;

struct sockaddr_in dst;
socklen_t adrLen;
memset(buff,0,128);
int count = 0;
sleep(2);
inet_aton("192.168.16.25",&adr);
dst.sin_addr=adr;
dst.sin_port=htons(CONNECTED_UDP_PORT);
dst.sin_family= AF_INET;
while(1)
{
printf("\nIO data function\n ");
/*len= recvfrom(nsfd,buff,1024,0,(struct sockaddr*)
&dst,sizeof(dst)); */
len= recv(nsfd,buff,1024,0);
printf("\n data function\n ");
if (len>0)
{
for (i=0;i<len;i++)
{
printf(" %x ",buff[i]);
}
udpPacket_t *udppkt= (udpPacket_t *)buff;
addrItem_t ai=udppkt->addrItem;
dataItem_t di=udppkt->dataItem;
addrData_t ad=ai.addrData;

printf("Type Id %x \n",ai.typeId);
printf("length %x\n",ai.length);
printf("ConnId %x \n",ad.connId);
printf("Seq no %x \n",ad.seqNo);
printf("Data typeid %x\n",di.typeId);
printf("Data length %x\n",di.length);
printf("Item Count %x\n",udppkt->itemCount);

}

}

outputs on both the machines
sending side prints

Itemcnt 2
adr type 8002
adr len 8
connid b5c06921
seq 0
data type id b1
data len 1
data 1

2 0 0 0 2 80 0 0 8 0 0 0 21 69 c0 b5 0 0 0 0 b1 0 0 0 1 0 0 0 1 UDP
PACKET SENT

this is recving side prints

recv data function

2 0 0 0 2 80 0 0 8 0 0 0 21 69 c0 b5 0 0 0 0 b1 0
0 0
1 0 0 0 1
<+==========================>
plz see here the recving data is changed even though the packet
captured is fine in order
<+======================>
Item Count 2
Type Id 0
length 8002
ConnId 80000
Seq no 69210000
Data typeid b5c0
Data length 0

IO data function
kindly suggest u r views to correct my errors
thanks and regards
PMM

May 11 '06 #1
1 4141
pmm wrote:
hi I am repeating my post here plz excuse

i am trying out a UDP packet
transfer between a windows machine and a linux I created a structure on
both sides (ie on linux and on windows) and I sent using UDP
but on the receiving side when I am trying to reform the packet back
into structure tis giving me different outputs I assume it is the
problem of structure padding I even used pragma pack (1) but still it
is not working out for me kindly do suggest u r view to execute this
Please don't use stupid contractions like u, r, plz. They make it a
*lot* harder to read your posts. Your use of such things may well be one
reason why some people don't bother responding.
correctly I am palcing structure all well capturing code on recving
side and also print statement of both the machines

kindly suggest me what to modify

my structure
<code>
typedef struct
{
INT32S connId;
You are still not providing complete compilable examples, something I'm
sure you have been advised to do. This is obvious because I've yet to
see a definition of INT32S.
INT32S seqNo;

}addrData_t;

typedef struct
{
INT16S typeId;
INT16S length;
addrData_t addrData;

}addrItem_t;

typedef struct
{
INT16S typeId;
INT16S length;
INT8U data[1];

}dataItem_t;

typedef struct
{
INT16S itemCount;
addrItem_t addrItem;
dataItem_t dataItem;

} udpPacket_t;

sending side code lines
INT8U *buff=GetMemBuff(MAX_DATA_LEN);
<code>
udpPacket_t *udpPacket=(udpPacket_t*)buff ;
I'm sure you have been advised that this is not a good way to do this.
You should manually build up the packets in to the buffer since the
compiler can insert padding between fields. Such padding may be
dependant on the compiler options you use and might change on the next
build. Also, there is absolutely no guarantee that the buffer returned
by GetMemBuff (something else we have seen no definition of) is
correctly aligned for your structure.
addrItem_t *ai=&udpPacket->addrItem;
<snip>
</code>

this is receving side code

void* IODataRecv(void* sfd)
{
int nsfd=*((int *)sfd);
int len ,i;
char *buff=GetMemBuff(128);


<snip>

I know for a fact I advised you to use an unsigned char buffer, yet you
are using a char buffer.

I'm not going to read the rest of your code since you have yet to deal
with the issues already raised with it.

Also, please don't email me code to comment on. I deal with C questions
on this group so that others get a chance to correct the mistakes that I
make. My private email address is only provided for when people have
reason to contact me about things not topical here, which occasionally
does happen.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
2
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
15
by: damian birchler | last post by:
Hi I'm wondering of what type a structure is. Of course, it is a _structure_, but an array isn't an _array_ either. So of what type is a structure? I'd say a pointer, am I right?
10
by: ranjeet.gupta | last post by:
Dear All !! Before i qoute my querry, I will like to qoute my analysis and my Knowledge Struct a { int raw; char data; };
28
by: kyle york | last post by:
Greetings, Why does the C standard require the members of a structure not be re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which means one cannot rely on the exact layout...
4
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler...
24
by: karthikbalaguru | last post by:
Hi, I find that the structure padding is not being taken into account while using 'new' operator. Is there a way to enable it ? struct Dataunit { char dataid; int keyid;
10
by: Rohit kumar Chandel | last post by:
Hi All, Please let me know how to find in a structure whether compiler has used padding or not. Regards Rohit
12
by: Kislay | last post by:
case 1 : struct s { char c1; // 8 double d; // 8 int i1; // 4 char c2; // 4 int i2; // 4 };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.