ctcplabwindows

labwindows: What argument does ServerTCPRead exactly need?


The Labwindows documentation says the following about the ServerTCPRead Method:

int ServerTCPRead (unsigned int conversationHandle, void *dataBuffer, size_t dataSize, unsigned int timeOut);

dataBuffer void * The pointer to the buffer in which to store the data.

However, when I actually declare a pointer and pass it to the method, it returns with the following error:

FATAL RUN-TIME ERROR: "test1.c", line 146, col 61, thread id 0x00001474: Array argument too small (1 bytes). Argument must contain at least 60000 bytes (60000 elements).

Here is the code I am using:

char * receiveBuf="";
    ssize_t dataSize = 60000;

    switch (event)
        {
        case TCP_CONNECT:

            break;
        case TCP_DATAREADY:
            if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf,
                                           dataSize, 1000))
                < 0)
                {
                //SetCtrlVal (g_hmainPanel, MAINPNL_RECEIVE, "Receive Error\n");

                }
            else
                {
            display_value_from_client(receiveBuf);

                }                                            
            break;

Solution

  • You have allocated receiveBuf as an empty string, so there is no space to store the data you would receive in the function call. I would replace

    char * receiveBuf="";
    ssize_t dataSize = 60000;
    

    with

    ssize_t dataSize = 60000;
    char * receiveBuf = malloc(dataSize);
    

    and don't forget to call free later as needed.

    Also based on the error this call may block until it receives dataSize bytes. You should check the documentation for that.