CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2008
    Posts
    4

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

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    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

  3. #3
    Join Date
    Mar 2001
    Posts
    2,529

    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);
    Quote Originally Posted by [email protected] View Post
    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.

  4. #4
    Join Date
    Sep 2008
    Posts
    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?

  5. #5
    Join Date
    Mar 2001
    Posts
    2,529

    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.

  6. #6
    Join Date
    Sep 2008
    Posts
    4

    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.

  7. #7
    Join Date
    Mar 2001
    Posts
    2,529

    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.

  8. #8
    Join Date
    Sep 2008
    Posts
    4

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

  9. #9
    Join Date
    Mar 2001
    Posts
    2,529

    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
  •  





Click Here to Expand Forum to Full Width

Featured