P
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);
}
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
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);
}
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