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. C / C++ / MFC
  4. ISSUE - TCP/IP sockets in MFC VS2017

ISSUE - TCP/IP sockets in MFC VS2017

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++sysadmin
9 Posts 5 Posters 0 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;    
    

    }

    Richard DeemingR L G V 4 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;    
      

      }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You have already posted this in the Managed C++/CLI forum, and been given an answer. ISSUE - TCP/IP sockets in MFC VS2017 - Managed C++/CLI Discussion Boards[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      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;    
        

        }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        D.Manivelan wrote:

        it showing some junk value.

        Where is it showing, and what value?

        D 1 Reply Last reply
        0
        • L Lost User

          D.Manivelan wrote:

          it showing some junk value.

          Where is it showing, and what value?

          D Offline
          D Offline
          D Manivelan
          wrote on last edited by
          #4

          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
          

          Output 190.000015 - Correct value 'Ô' - Junk value - Wrong 6.360e-39#DEN - Junk value - Wrong 1.401e-45#DEN - Junk value - Wrong

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

            }

            G Offline
            G Offline
            Graham Breach
            wrote on last edited by
            #5

            This function returns a pointer to a local variable. If you want UNI.Buffer to continue to be accessible outside the function you have to allocate memory for it somehow.

            1 Reply Last reply
            0
            • D D Manivelan

              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
              

              Output 190.000015 - Correct value 'Ô' - Junk value - Wrong 6.360e-39#DEN - Junk value - Wrong 1.401e-45#DEN - Junk value - Wrong

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Most likely you are not converting your values to/from network order when sending and receiving. See htonf function (Windows)[^].

              D 1 Reply Last reply
              0
              • L Lost User

                Most likely you are not converting your values to/from network order when sending and receiving. See htonf function (Windows)[^].

                D Offline
                D Offline
                D Manivelan
                wrote on last edited by
                #7

                The same code works fine in VS2008. And moreover the issue occurs when I try to initialize the union member before actually sending the data to the socket. My question is why I am not able to assign a float value to the union member which is also a float variable. This same line of code assigns the value in VS2008. Why its not doing it in VS2017

                UNI.S.fTestValue1 = fTest; //6.360e-39#DEN // Junk value - Wrong
                UNI.S.fTestValue2 = 190.000015f; //1.401e-45#DEN // Junk value - Wrong

                L 1 Reply Last reply
                0
                • D D Manivelan

                  The same code works fine in VS2008. And moreover the issue occurs when I try to initialize the union member before actually sending the data to the socket. My question is why I am not able to assign a float value to the union member which is also a float variable. This same line of code assigns the value in VS2008. Why its not doing it in VS2017

                  UNI.S.fTestValue1 = fTest; //6.360e-39#DEN // Junk value - Wrong
                  UNI.S.fTestValue2 = 190.000015f; //1.401e-45#DEN // Junk value - Wrong

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Well since we have no real idea of what your code is doing to produce these bad values, it is difficult to offer any further suggestions.

                  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;    
                    

                    }

                    V Offline
                    V Offline
                    Victor Nijegorodov
                    wrote on last edited by
                    #9

                    Just to extend the reply of Graham Breach: move the

                    union USendLive
                    {
                    struct SSend
                    {
                    float fTestValue1;
                    float fTestValue2;
                    char cChr;
                    }S;
                    unsigned char Buffer[LIVEUNISIZE];
                    }UNI;

                    to the CSendValue class ctor to make the UNI a class member. Then your CSendValue :: SendLiveValues() would look like

                    unsigned char* CSendValue :: SendLiveValues()
                    {
                    float fLocalValue;
                    float fTest;

                    fTest 		        = 190.000015f;
                    
                    fLocalValue	        = fTest;   	
                    UNI.S.cChr 	        = 'c';		
                    UNI.S.fTestValue1 	= fTest;	
                    UNI.S.fTestValue2 	= 190.000015f;	
                    
                        return UNI.Buffer;    
                    

                    }

                    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