12-16-2008 08:30 PM
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
Solved! Go to Solution.
12-17-2008 03:46 AM
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
12-17-2008 03:56 AM
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;
}
12-17-2008 06:26 AM
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
12-20-2008 05:16 PM
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
06-07-2016 06:56 AM
in my case evry time server will sen strucrure ...and eroor is coming...evry time tcp_datat_rdy signal will generate..pls help me
06-08-2016 06:35 PM
Hi sudhirvasudevacahrya
Could you provide a screenshot of the error you are seeing? I don't understand your question.
06-08-2016 09:09 PM
Error
ServertcpLbrary Error
messge :timeouteror
Sytem error:no Eror
06-09-2016 05:18 AM - edited 06-09-2016 05:19 AM
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.
06-09-2016 05:53 AM
okay Sure sir,kudos