|
-
March 13th, 2009, 04:07 AM
#1
Sending Struct over UDP
I want to send a STRUCT over UDP using CAsyncSocket, based on a demo class I found called CUdpAsySk. Sending strings work nicely. Then creating a struct:
//struct defined in CUdpAsySk.h
struct StoSend
{
int16 version;
double gps_lat;
double gps_long;
}
class CUdpAsySk : public CAsyncSocket
{
public:
StoSend*pStoSend;
StoSend*pStoReceive;
...
}
//Send struct in void CUdpAsySk: oAsyncSendBuff()
m_strRemote="192.168.1.5";
m_nPort=4000;
dwBytes = SendTo((const char *)&pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote);
//Receive
nRead = ReceiveFrom((char *)&pStoReceive, 699999, m_strRemote, m_nPort);
This work on local computer when bytes sent are equal or less then 20 bytes, from a remote computer no struct is received. When is failes I receive a:
Unhandled exception at 0x7c1772af (mfc71d.dll) in Device.exe: 0xC0000005: Access violation reading location 0x00000f94.
Anyone here that can help me with this problem?
Last edited by [email protected]; March 13th, 2009 at 04:10 AM.
-
March 13th, 2009, 11:24 AM
#2
Re: Sending Struct over UDP
Break into the debugger, and see where you are in the stack. There is not enough code provided to say. Also, please use code tags when posting code.
Viggy
-
March 14th, 2009, 07:06 PM
#3
Re: Sending Struct over UDP
Well just looking at your send command, you are sending a reference to a pointer. Should be:
Code:
dwBytes = SendTo((const char *)pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote);
 Originally Posted by [email protected]
dwBytes = SendTo((const char *)&pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote);
//Receive
nRead = ReceiveFrom((char *)&pStoReceive, 699999, m_strRemote, m_nPort);
Anyone here that can help me with this problem?
ahoodin
To keep the plot moving, that's why.

-
March 16th, 2009, 09:02 AM
#4
Re: Sending Struct over UDP
Do I need to convert the struct into bytes before sending it or is this the correct way of sending a struct over UDP?
-
March 16th, 2009, 09:26 AM
#5
Re: Sending Struct over UDP
Its fine.
Is your server echoing the bytes back?
nRead = ReceiveFrom((char *)&pStoReceive, sizeof(StoSend), m_strRemote, m_nPort);
Last edited by ahoodin; March 16th, 2009 at 09:35 AM.
ahoodin
To keep the plot moving, that's why.

-
March 17th, 2009, 03:46 AM
#6
Re: Sending Struct over UDP
dwBytes = SendTo((const char *)&pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote);
nRead = ReceiveFrom((char *)&pStoReceive, sizeof(StoSend), m_strRemote, m_nPort);
Sending a struct of 24 bytes local the pStoReceive is received correctly, however sending more then 24 bytes I get a error msg: Unhandled exception at 0x7c1772af (mfc71d.dll) in Device.exe: 0xC0000005: Access violation reading location 0x00000f94.
using: LAN PC to PC no data is transmitted correctly?
using: dwBytes = SendTo((const char *)pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote); no data is transmitted correctly?
Last edited by [email protected]; March 17th, 2009 at 04:44 AM.
-
March 17th, 2009, 07:47 AM
#7
Re: Sending Struct over UDP
If you are going to send an array you are going to have to multiply and reflect that in your sendto and recvfrom.
sizeof(StoSend) * nArrayElements
ahoodin
To keep the plot moving, that's why.

-
March 19th, 2009, 04:17 AM
#8
Re: Sending Struct over UDP
Finally found the correct way, the problem is unneeded casting and incorrect handling of pointers 
dwBytes = SendTo(pStoSend, sizeof(*pStoSend), m_nPort,m_strRemote);
nRead = ReceiveFrom(pStoReceive, sizeof(*pStoReceive), m_strRemote, m_nPort);
-
March 19th, 2009, 07:38 AM
#9
Re: Sending Struct over UDP
Well you really shouldn't use sizeof on a dereferenced pointer, you should use sizeof on a type.
Hence:
Code:
dwBytes = SendTo(pStoSend, sizeof(StoSend), m_nPort,m_strRemote);
nRead = ReceiveFrom(pStoReceive, sizeof(StoReceive), m_strRemote, m_nPort);
ahoodin
To keep the plot moving, that's why.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|