LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

tcp callback

Hi,

I tried to use the labwindows libs (7.0) in visual c++ 6 in order to communicate in TCP/IP.

I found an example : client.prj and server.prj.  I "translated" these examples into 2 vc6 projects win console application, client and server, (I just removed User Interface).  The client can connect to the server but, the registered callbacks ClientTCPCB and ServerTCPCBs are never called and I do not understand  why...

if I use "Labwindows exemple server" with "Labwindows exemple client" everything works fine.

if I use "Labwindows exemple server" with "my VC6 client" the server receives messages, but if I send a message from the server to the client, the callback on the client is not called.

if I use "my VC6  server" with "Labwindows exemple client" the callback on the server is not called.

 

here is the client code, libs used: cvirt.lib cvisupp.lib 

 

// rtti_TestClt.cpp : Defines the entry point for the console application.

#include <cvirte.h>
#include <stdio.h>
#include <stdlib.h>
#include <tcpsupp.h>
#include <string.h>
#include <utility.h>
#include <userint.h>
#include "rtti_testclt.h"

#define MAX_SRV 5
static unsigned int g_hconversations[MAX_SRV];

int main (int argc, char *argv[])
{
    int iErr= 0;
 int  portNums[MAX_SRV];
    char tempBuf[256] = {0};
 char portNumStrs[MAX_SRV][32];

 char ipHostStrBuf[256] = {0};
 char ipPeerStrBuf[256] = {0};
 char hostnameHostStrBuf[256] = {0};
 char hostnamePeerStrBuf[256] = {0};

    if (InitCVIRTE (0, argv, 0) == 0)
        return -1;

   // DisableBreakOnLibraryErrors();

 printf("select srv1 port:\n");
 scanf("%d", &portNums[0]);
 //printf("select srv2 port:\n");
 //scanf("%d", &portNums[1]);


 for (int i = 0; i<1;i++)
 {
  /* Attempt to connect to TCP server... */
  //pass the id of the connection in param: i in order to call the good callback on the server side
  if (ConnectToTCPServer (&g_hconversations[i], portNums[i], tempBuf, ClientTCPCB,
        (void*)i, 5000) < 0)
  {
   printf("TCP Client Connection to server failed !");
  }
  else
  {

   /* We are successfully connected -- gather info */

   GetTCPHostAddr (ipHostStrBuf, 256);
   GetTCPHostName (hostnameHostStrBuf, 256);
   GetTCPPeerAddr (g_hconversations[i], ipPeerStrBuf, 256);
   GetTCPPeerName (g_hconversations[i], hostnamePeerStrBuf, 256);

   printf("ip address :%s \nhostname:%s\n",ipPeerStrBuf,hostnameHostStrBuf);
   printf("ip address peer:%s \nhostname peer:%s\n",ipPeerStrBuf,hostnamePeerStrBuf);
   printf("handle server:%d, id = %d.(use id at the start of data to send):",g_hconversations[i],i);
 
  }
    }
 while (iErr == 0)
 {
  int iCli = 0;
  scanf("%80s", tempBuf);
  //iCli = tempBuf[0]-49;
  iCli = tempBuf[0];
  if (iCli == 'X')
  {
   iErr = 1;
  }
  else
  {
   TransmitCB(g_hconversations[iCli],tempBuf,256);
   
  }
 }
 for (int j = 0; j<MAX_SRV;j++)
 {
  /* Disconnect from the TCP server */
  DisconnectFromTCPServer (g_hconversations[j]);
    }
 CloseCVIRTE ();
    return 0;
}

/*---------------------------------------------------------------------------/
// When the user hits ENTER after typing some text, send it to the server... /
//---------------------------------------------------------------------------*/
int TransmitCB (unsigned int uiConversation,char* pTransmitBuf, int iSizeBuf)
{
 strcat (pTransmitBuf, "\n");
 int iErr = 0;
 iErr = ClientTCPWrite (uiConversation, pTransmitBuf,strlen (pTransmitBuf) + 1, 1000);
    if (iErr< 0)
 {
  printf ("Transmit Error %d \n",iErr); 
 }


    return 0;
}

/*---------------------------------------------------------------------------*/
/* This is the TCP client's TCP callback.  This function will receive event  */
/* notification, similar to a UI callback, whenever a TCP event occurs.      */
/* We'll respond to the DATAREADY event and read in the avaiable data from   */
/* the server and display it.  We'll also respond to DISCONNECT events, and  */
/* tell the user when the server disconnects us.                             */
/*---------------------------------------------------------------------------*/
int CVICALLBACK ClientTCPCB (unsigned handle, int event, int error,
                             void *callbackData)
{
    int  dataSize         = 256;
    char receiveBuf[256] = {0};
 int iServId = 0;
 iServId = (int)callbackData;

    switch (event)
        {
        case TCP_DATAREADY:
            if ((dataSize = ClientTCPRead (g_hconversations[iServId], receiveBuf,
                                           dataSize, 1000))
                < 0)
   {
                printf( "Receive Error\n");
   }
            else
   {
                printf ("rcv:%s", receiveBuf);
   }
            break;
        case TCP_DISCONNECT:
            printf("TCP Client: Server has closed connection!");
            g_hconversations[iServId] = 0;
            break;
    }
    return 0;
}

0 Kudos
Message 1 of 2
(3,656 Views)

For the CVI callback/events mechanism to work, there is normally a call to RunUserInterface() in your program which takes care of it. As you have removed the user interface and therefore the call to RunUserInterface(), you need to replace it with some other arrangement. ProcessTCPEvents() is probably the one you need, which should be called periodically to check the status of the TCP engines. It in turn will fire the callbacks as required.

 

JR

0 Kudos
Message 2 of 2
(3,653 Views)