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;
}
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.
-
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;
}
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.