I'm writing a program for WinCE7, and I'm trying to setting up a static library class that will be used by the program for TCP communication. Different member variable orders, thus, stuck the program in different ways. Parts of the code:
Header:
FxTcp.h
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
class FxServer {
// Variables
SOCKET s ;
int bytesExpected ;
int bytesToReceive ;
int cbXfer ;
int cbTotalRcvd ;
int cbRemoteAddrSize ;
fd_set fdReadSet ;
LPWSTR lastError ;
SOCKADDR_STORAGE ssRemoteAddr ;
public:
// Constructors
FxServer() ;
FxServer( char * serverAddr ) ;
FxServer( char * serverAddr , char * serverPort ) ;
// Methods
int connect() ;
int deviceRead( char devType , bool multiDev , char * pBuf , UINT32 first , UINT16 numDev = 1 ) ;
void release() ;
} ;
Source:
#include "StdAfx.h"
#include "MelsecFxTcp.h"
int FxServer::deviceRead( char devType , bool multiDev , char * pBuf , UINT32 first , UINT16 numDev ) {
int cbXfer = 0 ;
// PARSE INPUTS...
// COMPOSE AND SEND MESSAGE...
// RECEIVE AND PARSE RESPONSE
FD_ZERO( &fdReadSet ) ;
FD_SET( s , &fdReadSet ) ;
cbRemoteAddrSize = sizeof( ssRemoteAddr ) ;
// Receive the first 2 bytes...
cbTotalRcvd = 0 ;
bytesToReceive = 2 ;
do {
cbXfer = recvfrom( s , pBuf + cbTotalRcvd , sizeof(char)*2 , 0 , (SOCKADDR*)&ssRemoteAddr , &cbRemoteAddrSize ) ;
cbTotalRcvd += cbXfer ;
} while ( cbXfer > 0 && cbTotalRcvd < bytesToReceive ) ;
In this case the program freezes exactly at the call of the recvfrom function. But if I switch the order of the last 2 member variables in the header, like this:
SOCKADDR_STORAGE ssRemoteAddr ;
LPWSTR lastError ;
Then the program will go on receiving the bytes, but it will freeze again when trying to use the string during the execution of the method some lines later, as in the example below:
StringCchPrintf( lastError , sizeof(char)*256 , TEXT("Message for the user") ) ;
Everything was running perfectly fine in my tests where the communication code was in the main program (and I didn't use LPWSTR strings).
I'm sure it is a stupid trivial concept I'm missing but I cannot solve this.
My gratitude to anyone that could help.