LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Command passing from client to Server

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

 

0 Kudos
Message 1 of 19
(4,241 Views)

Which error are you receving? Can we see the code where you call that deferred function?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 19
(4,238 Views)

 

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

 

 

 

 

0 Kudos
Message 3 of 19
(4,237 Views)

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;
}
0 Kudos
Message 4 of 19
(4,238 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 5 of 19
(4,232 Views)

for this i need your help sir

0 Kudos
Message 6 of 19
(4,206 Views)

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


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 7 of 19
(4,202 Views)

okay I wil try this 

0 Kudos
Message 8 of 19
(4,199 Views)

  memset (&cI, 0, sizeof (ClientInfo));   

 

this line giiving eror saying tht cI Argument too small

0 Kudos
Message 9 of 19
(4,179 Views)

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
0 Kudos
Message 10 of 19
(4,180 Views)