06-09-2016 02:01 AM - edited 06-09-2016 02:03 AM
The actual code you posted does not assign any value to cI->threadID so you are posting the deferred call to thread 0 which does not exist so nobody is processing deferred call events and the queue gets full in a couple runs.
06-09-2016 08:30 AM
still Iam getting timeout eror in servertcpread side even though iam not sending any data from client side
06-09-2016 08:34 AM - edited 06-09-2016 08:35 AM
Set a breakpoint in the server function and see how the deferred function is called, next place a breakpoint in the reader function and see if it is called only and every time the server function called it. If you do not want to interrupt the program you could add some DebugPrintf command to output diagnostic messages do the debug output window. In the diagnostic messages trace the conversation handle use on both sides so that you can verify it is the same.
06-09-2016 08:44 AM
Still Iam getting time out eror and some time stackfull error some time fnDeferredReceive callback ques is full message is coming /*from clinet side i will send 7 tcp write My Requirement multiple commad should send from client to server ,if my datbuffer is filled 200 bytes i will execute one commad*/ for(i=0;i<7;i++) { TDP.bSendRequestedData = FALSE; TDP.uPacketType = TCP_CMD_DRIVE_STATUS; TDP.uPacketSize = sizeof(COMMAND_GDSCALLS); TDP.uOffset = 0; CMD_GDS.u[0] = TCP_CMD_DRIVE_STATUS; CMD_GDS.u[1] = 0; CopyBytes(TDP.sDataBuffer, 0, (char *)&CMD_GDS, 0, TDP.uPacketSize); sDataPtr = (char *)&TDP; iDataSize = TDP.uPacketSize + STR_VARIABLES * sizeof(unsigned); ClientTCPWrite (gConversationHandle, sDataPtr, 216, 0); } /* Server side shoul recieve same commad one by one */ 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) { uhLogFile = OpenFile ("C:\\MTL32_2020\\ServerClientTCPCheck.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); sprintf (cLogLine, "[%s] [%s] TCP_CONNECT ",DateStr(),TimeStr()); WriteLine (uhLogFile, cLogLine, strlen(cLogLine)); /* 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) { memset (cI, 0, sizeof (ClientInfo)); cI->handle = handle; cI->threadId= clientInfoPtr->threadId ; //------------------------------------ cI->handle=clientInfoPtr->handle; /* TCP conversation handle */ cI->threadId=clientInfoPtr->threadId; /* ID of worker thread */ cI->threadFuncId=clientInfoPtr->threadFuncId; /* ID of worker thread function */ cI->stopFlag=clientInfoPtr->stopFlag; /* Flag used to stop worker thread */ memcpy(&cI->name[0],&clientInfoPtr->name[0],256); /* Descriptive name of client connection */ cI->readingData=clientInfoPtr->readingData; /* Indicates server is reading from client */ cI->panel=clientInfoPtr->panel; /* Handle of panel to display client data */ //-------------------------------------- // Fill all necessary structure field PostDeferredCallToThread (fnDeferredReceive, (void *)cI,cI->threadId); // DO NOT dispose of the dynamic memory! // clientInfoPtr->readingData = 1; //PostDeferredCallToThread (fnDeferredReceive, clientInfoPtr,clientInfoPtr->threadId); } } } return 0; } static void CVICALLBACK fnDeferredReceive (void *callbackData) { int error = 0; ClientInfo *cI = 0; //-------------------------SUDHIR 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 if (!callbackData) { // Properly handle this error! error = 1; goto Error; } cI = (ClientInfo *)callbackData; /* * Disable library error checking as we are going to read until * there is no more data left (read call times out). */ /* Read the Request. */ //aft delete test sudhir------------------ dataSize = bytesToRead = TDP.uPacketSize; dataSize = bytesToRead = 200; while (bytesToRead > 0) { //------------------------------------- iReturn =ServerTCPRead (cI->handle, &TDP.bSendRequestedData, sizeof (TDP.bSendRequestedData), 50000);//was 10000 iTotalByteRxvd=iTotalByteRxvd+iReturn; tcpChk(iReturn); /* Read the Paket Type. */ iReturn =ServerTCPRead (cI->handle, &TDP.uPacketType, sizeof (TDP.uPacketType), 10000) ; iTotalByteRxvd=iTotalByteRxvd+iReturn; SetCtrlVal(pnMTL32Server,PN_SERVER_NUM_PKTTYPE,TDP.uPacketType); //packet type sprintf (cLogLine, "%s [%s] tcpChk for ServerTCPReading TDP.uPacketType:%i:",DateStr(),TimeStr (), TDP.uPacketType); WriteLine (uhLogFile, cLogLine, strlen(cLogLine)); if(iReturn <0) { uhLogFile = OpenFile ("C:\\MTL32_2020\\ServerClientTCPCheck.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); sprintf (cLogLine, "%s [%s] tcpChk for ServerTCPReading TDP.uPacketType ,Error Number %i:",DateStr(),TimeStr (), iReturn); WriteLine (uhLogFile, cLogLine, strlen(cLogLine)); } tcpChk(iReturn); /* Read the Paket Size. */ iReturn = ServerTCPRead (cI->handle, &TDP.uPacketSize, sizeof (TDP.uPacketSize), 10000); iTotalByteRxvd=iTotalByteRxvd+iReturn; tcpChk(iReturn); /* Read the offset. */ iReturn = ServerTCPRead (cI->handle, &TDP.uOffset, sizeof (TDP.uOffset), 10000) ; iTotalByteRxvd=iTotalByteRxvd+iReturn; tcpChk(iReturn); if(TDP.bSendRequestedData) { cI->readingData = 0; fnSendClientRequestedData(cI->handle); } else if(TDP.uPacketSize > 0) /* Read the data. Note that the data could be empty. */ { DisableBreakOnLibraryErrors (); DisableBreakOnLibraryErrors (); //while (bytesToRead > 0) // { //bytesRead = ServerTCPRead(clientInfoPtr->handle,&TDP.sDataBuffer[dataSize - bytesToRead], bytesToRead, 10000); bytesRead = ServerTCPRead(cI->handle,&TDP.sDataBuffer[dataSize - bytesToRead], 200, 10000); sprintf (cLogLine, "%s [%s] TDP.sDataBuffer bytesRead = %i:",DateStr(),TimeStr (), bytesRead); WriteLine (uhLogFile, cLogLine, strlen(cLogLine)); iTotalByteRxvd=iTotalByteRxvd+bytesRead; uTotal= uTotal+bytesRead; SetCtrlVal(pnMTL32Server,PN_SERVER_NUM_BYTE_RX,iTotalByteRxvd); //uPacketSize if (bytesRead > 0) bytesToRead -= bytesRead; else if (bytesRead != -kTCP_TimeOutErr) { uhLogFile = OpenFile ("C:\\MTL32_2020\\ServerClientTCPCheck.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII); tcpChk (bytesToRead); } if(uTotal >200 | uTotal ==200 ) { uTotal1=200 - uTotal; bytesToRead=200; ExecuteRequestedAction(&TDP); //memcpy(&TDP1.sDataBuffer[0],&TDP.sDataBuffer[100000],uTotal1); //memcpy(&TDP.sDataBuffer[0],&TDP1.sDataBuffer[0],uTotal1) ; } } //while (bytesToRead > 0) ending cI->readingData = 0; EnableBreakOnLibraryErrors (); } Error: if (cI) free (cI); // Free dynamic memory if (error) { // Handle errors in the function } return; }
06-09-2016 09:33 AM
Your code is quite coplex and I have no informations to argue about it.
Nevertheless, I see you are sending 7 fixed-size packets but you are reading each packet field-by-field. I would initially simplyfy the code and see if I receive all the packets sent, using a simple loop that iterates reading 216-bytes packets and logging them to the debug output window or to a file. This will clarify whether the structure you have setup is correct. Keep in mind that you may need to iterate on reading to get a whole packet.
If the structure is correct and no error is received, then you may go on adding some details more: are you sure reading field-by-field is mandatory or useful? Since packets have fixed size, couldn't you simply decode a whole packet at once?
Finally, it seems to me that the list of clients is stored in global memory and rather fixed (or at least it does not change order from call to call): if this is true the you could simply pass the index of the record to the deferred callback, leaving to that one to get the element from the list and use it.
06-09-2016 09:51 AM
Iam recieving 7 time 216 bytes in server side i odnt have problem in that
but after reading process complete Iam not sending any commad from client still flow is going inside While(bytesToRead) and empty data is is there while ServerTcpRead and return value is -11 nothing but timeout error
static void CVICALLBACK fnDeferredReceive (void *callbackData) { while (bytesToRead > 0) { iReturn =ServerTCPRead (cI->handle, &TDP.bSendRequestedData, sizeof (TDP.bSendRequestedData), 0);
06-09-2016 10:18 AM
The problem may be here:
if (uTotal >200 | uTotal ==200 ) // Wrong! Bitwise OR operator if (uTotal >200 || uTotal ==200 ) // Correct: logical OR operator if (uTotal >=200) // Even better { uTotal1=200 - uTotal; bytesToRead=200; <=== This maintains the while { bytesToRead>0 } active! ExecuteRequestedAction(&TDP); }
06-09-2016 11:06 AM
sorry sir how to prevent this type of error,this error occur beacuse of pressusized and short time coding d......
how to avoid this type of errors? Please suggest me
06-09-2016 03:07 PM
Unfortunately there is no simple rule to avoid that kind of things
The experience helps but is not a guarantee: you'll still make errors but you'll be more able to detect and correct them.