06-07-2016 04:08 AM
Dear All,
I have Problem with transferring structure from client to Server…
Big Problem is I am Using Android in client Side, server is windows using cvi5.5 in my source code they used
/*---------------------------------------------------------------------------*/ /* Receives data from client. */ /*---------------------------------------------------------------------------*/ static void CVICALLBACK fnDeferredReceive (void *data) { ClientInfoPtr clientInfoPtr = (ClientInfoPtr) data; char dataBuf[256]; int bytesRead, dataSize, bytesToRead, tcpErr = 0, iReturn,iTotalByteRxvd=0,i ; unsigned short uTotal =0,uTotal1=0,index=0; //assert (clientInfoPtr->readingData == 1); //commented by sudhir 26-05-2016 /* * Disable library error checking as we are going to read until * there is no more data left (read call times out). */ //DisableBreakOnLibraryErrors (); /* Read the Request. */ dataSize = bytesToRead = TDP.uPacketSize; dataSize = bytesToRead = 200; while (bytesToRead > 0) { //------------------------------------- iReturn =ServerTCPRead (clientInfoPtr->handle, &TDP.bSendRequestedData, sizeof (TDP.bSendRequestedData), 50);//was 10000 in Above code they used ASSERT so its giving problem while
in Cleint side (android side) he will send multiple tcpwrite example
tcpwrite1
tcpwrite2
so in server side its giving eror..so I commented assert so now my progrmm is working but evry time I will get error messge and if he not sending any commad also still evry time my fndefferdrxv will excute pls help me
06-07-2016 07:00 AM
Which error are you receving? Can we see the code where you call that deferred function?
06-07-2016 07:38 AM - edited 06-07-2016 07:43 AM
static void CVICALLBACK fnDeferredReceive (void *data) { ClientInfoPtr clientInfoPtr = (ClientInfoPtr) data; char dataBuf[256]; int bytesRead, dataSize, bytesToRead, tcpErr = 0, iReturn,iTotalByteRxvd=0,i ; unsigned short uTotal =0,uTotal1=0,index=0; unsigned short read_total ;//sudhir assert (clientInfoPtr->readingData == 1); //commented by sudhir 26-05-2016 /* * Disable library error checking as we are going to read until * there is no more data left (read call times out). */ /* Read the Request. */ dataSize = bytesToRead = TDP.uPacketSize; read_total=0; while (read_total <200) { iReturn =ServerTCPRead (clientInfoPtr->handle, &TDP.bSendRequestedData,sizeof (TDP.bSendRequestedData), 50000);//was 10000 iTotalByteRxvd=iTotalByteRxvd+iReturn; tcpChk(iReturn);
For this code iam getting
Erro
ServertcpLbrary Eror
messge :timeouteror
Sytem eror:no Eror
Iam Not sending any Clenttcpwrt from Clinet(my clinet is android)still fnDeferredReceive inside my flow is coming
06-07-2016 07:40 AM - edited 06-07-2016 07:42 AM
fnDeferredReceive callling from this below code
/*---------------------------------------------------------------------------*/ /* TCP callback function for the server. */ /*---------------------------------------------------------------------------*/ static int CVICALLBACK fnServerTCPCallback (unsigned int handle, int xType, int errCode, void *cbData) { static int i = 0; if (xType == TCP_CONNECT) { /* Connect new client. */ fnConnectClient (handle); bDAQClientReqMade = TRUE; } else if (xType == TCP_DISCONNECT) { /* Client is disconnecting. Update program state. */ DisconnectClient (handle); } else if (xType == TCP_DATAREADY) { ClientInfo clientInfo = {0}; ClientInfoPtr clientInfoPtr = &clientInfo; int index; /* Find the client information from TCP conversation handle.*/ clientInfoPtr->handle = handle; index = ListFindItem (gClientList, &clientInfoPtr, FRONT_OF_LIST, fnCompareClientInfoPtr); clientInfoPtr->readingData = 0; if (index > 0) { /* Get the stored client information. */ ListGetItem (gClientList, &clientInfoPtr, index); /* * NOTE - Because the reading is done in the worker thread, * this thread (the main thread) is not blocked, and will * continue to receive TCP_DATAREADY events, until all the * data is read. This program uses the readingData flag to * ignore these events, until all the data is read by the * worker thread.*/ //i = clientInfoPtr->readingData = 0; if (clientInfoPtr->readingData == 0) { clientInfoPtr->readingData = 1; PostDeferredCallToThread (fnDeferredReceive, clientInfoPtr, clientInfoPtr->threadId); } } } return 0; }
06-07-2016 08:40 AM - edited 06-07-2016 08:41 AM
It seems to me that the problem lies in ClientInfo and ClientInfoPtr variables being passed to the deferred callback. You must keep in mind that deferred callbacks are execute after the calling function terminates: at this moment, all variables local to the calling function (like your ClientInfo and ClientInfoPtr) are cleared and the pointer received by the deferred callback no longer points to a valid structure in memory. When the deferred callback executes, it points to a invalid memory block, so the handle you retrieve does not refers to an actual TCP connection and you get the timeout error.
If you want to maintain your program structure you could dynamically allocate the memory with malloc inside the calling function, pass the pointer to the deferred callback and free it there: I have already handled some applications with this paradigm with success, if properly configured no memory leak can occurr.
06-08-2016 06:15 AM - edited 06-08-2016 06:16 AM
for this i need your help sir
06-08-2016 07:40 AM
Something along this line.
The calling function (TCP callback):
static int CVICALLBACK fnServerTCPCallback (unsigned int handle, int xType, int errCode, void *cbData) { ClientInfo *cI = NULL; // ... cI = malloc (sizeof (ClientInfo)); if (xs == NULL) { // Out of memory // Properly handle this error! } else { memset (&cI, 0, sizeof (ClientInfo)); cI->handle = handle; // Fill all necessary structure field PostDeferredCallToThread (fnDeferredReceive, (void *)cI, clientInfo->threadId); // DO NOT dispose of the dynamic memory! } // ... return 0; }
The deferred function:
static void CVICALLBACK fnDeferredReceive (void *callbackData) { int error = 0; ClientInfo *cI = 0; if (!callbackData) { // Properly handle this error! error = 1; goto Error; } cI = (ClientInfo *)callbackData; // Handle data content Error: if (cI) free (cI); // Free dynamic memory if (error) { // Handle errors in the function } return; }
06-08-2016 08:38 AM
okay I wil try this
06-08-2016 11:08 PM
memset (&cI, 0, sizeof (ClientInfo));
this line giiving eror saying tht cI Argument too small
06-09-2016 12:11 AM
The deferred call back ques is ful msg error is coming for this code
/*---------------------------------------------------------------------------*/ /* TCP callback function for the server. */ /*---------------------------------------------------------------------------*/ static int CVICALLBACK fnServerTCPCallback (unsigned int handle, int xType, int errCode, void *cbData) { static int i = 0; ClientInfo *cI = NULL;//sudhir cI = malloc (sizeof (ClientInfo));//sudhir if (xType == TCP_CONNECT) { /* Connect new client. */ fnConnectClient (handle); bDAQClientReqMade = TRUE; } else if (xType == TCP_DISCONNECT) { /* Client is disconnecting. Update program state. */ DisconnectClient (handle); } else if (xType == TCP_DATAREADY) { ClientInfo clientInfo = {0}; //ClientInfo cI = &clientInfo; ClientInfoPtr clientInfoPtr = &clientInfo; int index; memset (cI, 0, sizeof (ClientInfo)); cI->handle = handle; PostDeferredCallToThread (fnDeferredReceive, (void *)cI,cI->threadId); } return 0; } /*---------------------------------------------------------------------------*/ /* Receives data from client. */ /*---------------------------------------------------------------------------*/ //static void CVICALLBACK fnDeferredReceive (void *data) //commented by sudhir static void CVICALLBACK fnDeferredReceive (void *callbackData) { char dataBuf[256]; int bytesRead, dataSize, bytesToRead, tcpErr = 0, iReturn,iTotalByteRxvd=0,i ; unsigned short uTotal =0,uTotal1=0,index=0; unsigned short read_total ;//sudhir //----------------------- int error = 0; ClientInfo *cI = 0; if (!callbackData) { error = 1; goto Error; } cI = (ClientInfo *)callbackData; //------------------------ //ClientInfoPtr clientInfoPtr = (ClientInfoPtr) data; cI->readingData = 1; // assert (clientInfoPtr->readingData == 1); //commented by sudhir 26-05-2016 /* * Disable library error checking as we are going to read until * there is no more data left (read call times out). */ /* Read the Request. */ dataSize = bytesToRead = TDP.uPacketSize; read_total=0; while (read_total <200) { //aft delete test sudhir------------------ iReturn =ServerTCPRead (cI->handle, &TDP.bSendRequestedData,sizeof (TDP.bSendRequestedData), 50000);//was 10000