ISSUE - TCP/IP sockets in MFC VS2017
-
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;
}
-
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;
}
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
-
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;
}
-
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
-
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;
}
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.
-
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
-
Most likely you are not converting your values to/from network order when sending and receiving. See htonf function (Windows)[^].
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 -
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 -
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;
}
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;
}