Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. ISSUE - TCP/IP sockets in MFC VS2017

ISSUE - TCP/IP sockets in MFC VS2017

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++sysadmin
3 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D Manivelan
    wrote on last edited by
    #1

    Hi, I have a server client application using TCP/IP sockets in MFC VS2017. Originally i have written the code in VS2008. Kindly see the below code which sends the data to client. But this same code gives me some problem in MFC VS2017. In the below code when I assign the value 190.000015f to a local variable, its taking the value whereas when I assign it to the union member variable

    UNI.S.fTestValue1

    and

    UNI.S.fTestValue2

    , it showing some junk value. Please help me to fix the problem

    unsigned char* CSendValue :: SendLiveValues()
    {
    union USendLive
    {
    struct SSend
    {
    float fTestValue1;
    float fTestValue2;
    char cChr;
    }S;
    unsigned char Buffer[LIVEUNISIZE];
    }UNI;

    memset(UNI.Buffer,0,LIVEUNISIZE);
    
    float fLocalValue;
    float fTest;
    
    fTest 		        = 190.000015f;
    
    fLocalValue	        = fTest;   		//190.000015    // Correct value
    UNI.S.cChr 	        = 'c';			//'Ô'           // Junk value - Wrong
    UNI.S.fTestValue1 	= fTest;		//6.360e-39#DEN // Junk value - Wrong
    UNI.S.fTestValue2 	= 190.000015f;		//1.401e-45#DEN // Junk value - Wrong
    
        return UNI.Buffer;    
    

    }

    J J 2 Replies Last reply
    0
    • D D Manivelan

      Hi, I have a server client application using TCP/IP sockets in MFC VS2017. Originally i have written the code in VS2008. Kindly see the below code which sends the data to client. But this same code gives me some problem in MFC VS2017. In the below code when I assign the value 190.000015f to a local variable, its taking the value whereas when I assign it to the union member variable

      UNI.S.fTestValue1

      and

      UNI.S.fTestValue2

      , it showing some junk value. Please help me to fix the problem

      unsigned char* CSendValue :: SendLiveValues()
      {
      union USendLive
      {
      struct SSend
      {
      float fTestValue1;
      float fTestValue2;
      char cChr;
      }S;
      unsigned char Buffer[LIVEUNISIZE];
      }UNI;

      memset(UNI.Buffer,0,LIVEUNISIZE);
      
      float fLocalValue;
      float fTest;
      
      fTest 		        = 190.000015f;
      
      fLocalValue	        = fTest;   		//190.000015    // Correct value
      UNI.S.cChr 	        = 'c';			//'Ô'           // Junk value - Wrong
      UNI.S.fTestValue1 	= fTest;		//6.360e-39#DEN // Junk value - Wrong
      UNI.S.fTestValue2 	= 190.000015f;		//1.401e-45#DEN // Junk value - Wrong
      
          return UNI.Buffer;    
      

      }

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      Probably a problem of structure packing / data alignment. With C/C++ structures and unions, member data are aligned by inserting padding bytes. How many bytes are inserted depends on the used compiler options where the default settings depend on the platform (CPU type and bit width). When sending such structures via network to other systems, you must ensure that sender and receiver use the same packing / alignment. This is usally done by setting the alignment to one byte. With Visual Studio use the #pragma pack[^] directive:

      // Save current setting and select 1 byte
      #pragma pack(push, 1)
      union USendLive
      {
      struct SSend
      {
      float fTestValue1;
      float fTestValue2;
      char cChr;
      } S;
      unsigned char Buffer[LIVEUNISIZE];
      } UNI;
      // Restore previous setting
      #pragma pack(pop)

      Note that this might fail with existing (old) versions of your server and client. If so and you have to support old versions, you must check which alignment has been used and select that for packing. BTW: This forum is for managed C++ / CLI but your post would fit in the C++ / MFC forum.

      1 Reply Last reply
      0
      • D D Manivelan

        Hi, I have a server client application using TCP/IP sockets in MFC VS2017. Originally i have written the code in VS2008. Kindly see the below code which sends the data to client. But this same code gives me some problem in MFC VS2017. In the below code when I assign the value 190.000015f to a local variable, its taking the value whereas when I assign it to the union member variable

        UNI.S.fTestValue1

        and

        UNI.S.fTestValue2

        , it showing some junk value. Please help me to fix the problem

        unsigned char* CSendValue :: SendLiveValues()
        {
        union USendLive
        {
        struct SSend
        {
        float fTestValue1;
        float fTestValue2;
        char cChr;
        }S;
        unsigned char Buffer[LIVEUNISIZE];
        }UNI;

        memset(UNI.Buffer,0,LIVEUNISIZE);
        
        float fLocalValue;
        float fTest;
        
        fTest 		        = 190.000015f;
        
        fLocalValue	        = fTest;   		//190.000015    // Correct value
        UNI.S.cChr 	        = 'c';			//'Ô'           // Junk value - Wrong
        UNI.S.fTestValue1 	= fTest;		//6.360e-39#DEN // Junk value - Wrong
        UNI.S.fTestValue2 	= 190.000015f;		//1.401e-45#DEN // Junk value - Wrong
        
            return UNI.Buffer;    
        

        }

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        I question how you are determining it is wrong. You code does not demonstrate that. HOWEVER.... You are declaring a data entity on the stack. Then you are RETURNING part of that data entity from the method. That is ALWAYS wrong. And I suspect that is your problem. The calling code also uses the stack. And will reuse exactly the same memory as where you are attempting to put a value if it wants to. For example if you are calling a method to 'print' the returned value in some way. Thus overwriting it. And that would cause a "junk" value either now or in the future.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups