LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing a single structure via Client/ServerTCPRead/Write functions and insuring that all the data is transferred

Solved!
Go to solution

I am using the CVI TCP support package with my application and I am curious about the following code:

 

ClientTCPRead

char * buffer;
int messageSize;
int bytesToRead;
int bytesRead;

/* Find messageSize and allocate buffer appropriately... */

bytesToRead = messageSize;

while (bytesToRead > 0)

{

     bytesRead = ClientTCPRead (connectionHandle, &buffer[messageSize - bytesToRead], bytesToRead, 0);

     bytesToRead -= bytesRead;

}

 

 

Ok, this works if you are passing char array elements, but what if you were passing a structure of arbitrary size?  If you read/write the bytes read/written and you don't get all the data you requested, what do you do at this point to retrieve the rest of the subsequent data?  For example, replace the 'buffer' array of type char with a structure of a user-defined type with a size of 100 bytes or something to that extent.  You make a read/write request and read/wrote less than 100 bytes.  How do you get the rest of the data?  Does CVI do something in the background?  I could use this code with multiple structures, but then again, a particular member of a structure is not the size of a byte like a char.  

 

Much appreciated,

 

Chris

 

0 Kudos
Message 1 of 11
(5,674 Views)

I solved this problem by packaging up the sample code inside another function. So, at the point this function is called, cast your array/structure/whatever to a void* for processing as shown.

 

JR

0 Kudos
Message 2 of 11
(5,665 Views)

the second parameter to ClientTCPRead is declared as a pointer to anything (void *), and the third parameter is a length expressed in bytes. the fact that you are using a char buffer does not modify the way the function stores the data into your buffer. so, you will have to use pointer arithmetic on byte arrays, by first converting your buffer to a byte array.

 

your code will then look like this:

bytesRead = ClientTCPRead (connectionHandle, (unsigned char *)buffer + bytesToRead, bytesToRead, 0);

and bytesToRead is to be initialized with n*sizeof( yourMessageStructure ).

 

here is loop i use for receiving one message defined as a structure:

 

typedef struct

{

    .... // some message fields here

} message message;


message buffer;

int read_total, read;

 

read_total = 0;

while ( read_total < sizeof( message ) )

{

    read = ClientTCPRead( connection, (unsigned char *)buffer + read_total, sizeof( message ) - read_total, 10 )

    if ( read < 0 )

    {

         // handle error here

    }

    read_total += read;

}

 

 

0 Kudos
Message 3 of 11
(5,663 Views)

Thanks.  I agree with this method using pointer arithmetic.  So, the 'read_total' acts as an offset into the location of any arbitrary structure passed to this TCP function?  Nice.  I will try to implement this method into my application, and see what results I come up with.  It looks as if it should work, and I understand what's going on, so nothing left but to give it a shot.  I appreciate your help. 

 

Much appreciated,

 

Chris

0 Kudos
Message 4 of 11
(5,652 Views)
Solution
Accepted by Chrisg101080

The solution is to use a char casted pointer to allocate the transmitted data.  The only catch is that buffer must be a pointer to the data type of the structure before a char cast can be used for a successful program compilation. Thank you for your help. 

 

Chris

0 Kudos
Message 5 of 11
(5,597 Views)

in my case evry time server will sen strucrure ...and eroor is coming...evry time tcp_datat_rdy signal will generate..pls help me

0 Kudos
Message 6 of 11
(4,641 Views)

Hi sudhirvasudevacahrya

 

Could you provide a screenshot of the error you are seeing? I don't understand your question.

 

Andy F.
Applications Engineering
National Instruments
0 Kudos
Message 7 of 11
(4,625 Views)

 

Error

ServertcpLbrary Error

messge :timeouteror

Sytem error:no Eror

 

 

 

0 Kudos
Message 8 of 11
(4,621 Views)

Andy, this question refers to this other thread: you may find better to join us in that discussion.

 

Sudhir, as you can see, spreading a single question over multiple threads ingenerates confusion; you are asked to repeatedly supply details and users can not be aware of other answers and suggestions you may have received elsewere. It is a good practice to keep discussion in one thread only: int he future please stick to this.



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?
Message 9 of 11
(4,612 Views)