LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

TCP/IP callback function

Hi,

 

I am working on TCP/IP connection where my external device acts as server. I am using client.prj program as my reference.Sample code works fine in which i can able to read the data coming out from server side for every 2s and if i make any request from client side proper response is coming,

In my program i need to read reponse sends out from server at every 2s.I have kept thread where the specific thread periodically calls ProcessTCPEvents() and reads the data coming into that port.Control goes only one time to the TCPDATAREADY event even if i call ProcessTCPEvents() frequently in thread.

When i run with the client.prj code i can able to read continously. Please find my code below and put me some idea were i am making mistakes so that i can correct to read the data continously untill i disconnect my server connection.

 

    if (ConnectToTCPServer (&g_hconversation, GatewayPort, GatewayIP, ClientTCPCB,NULL, 5000) < 0)
        {
   MessagePopup("TCP Client", "Connection to server failed !");
   return 0;
  }
 
  else
        {
   CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, TCPDataRead,  NULL,&intThreadID2);  
   return 1;
  }

 

int CVICALLBACK TCPDataRead (void *FunctionData)
{

 while(1)
 {
  ProcessTCPEvents();
 }
 
 return 0;

int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error,
                             void *callbackData)
{
    char receiveBuf[256] = {0};
 char buffer[256]={0};
 int len;
 static int count=0;
    ssize_t dataSize         = sizeof (receiveBuf) - 1;
    switch (event)
        {
        case TCP_DATAREADY:
   {
   if ((dataSize = ClientTCPRead (g_hconversation, receiveBuf,
                                           dataSize, 2000))
                < 0)
                {
    dataSize=255;
                }
            else
                {
    fp = fopen(gsmmsgpath,"a+");  
    getsystemtime(systemtime);
    fwrite(systemtime,1,strlen(systemtime),fp); 
    strcpy(buffer,receiveBuf);
    fwrite(buffer,1,256,fp);
    dataSize=255;
    fclose(fp);
                }
   ProcessTCPEvents();
  
            break;
   }
        case TCP_DISCONNECT:
            MessagePopup ("TCP Client", "Server has closed connection!");
            g_connected = 0;
            break;
    }
 ProcessTCPEvents();
    return 0;
}


 

 

0 Kudos
Message 1 of 1
(3,611 Views)