08-07-2014 05:55 PM - edited 08-07-2014 06:01 PM
Hello,
I'm using LabWindows/CVI 2012 and I have to create a dll that passes data to/from LabVIEW (Call Library Function). The reason I am using a dll instead of LabVIEW for this functionality is that I have a lot of message processing to perform with variable sized UDP datagrams, etc. I really do not want to use the build in UDP Support Library calls in LabWindows/CVI and was hoping just to use ansi.c and winsock2 for portability unless someone informs me otherwise. Most of my code seems to work except I have spent several days trying to get the recvfrom and sendto functions to work at the same time.
My problem is that recvfrom is in an infinite loop. I have already tried setting the receive socket to non-blocking with no luck. I think the problem is that my far end application is sending datagrams at a rate that my dll cannot keep up. There is always data in the recv buffer so I never get to the sendto call below. I can post more, but this is the basic code (sorry, but the code is on a stand-alone not connected to Internet):
if (timeSinceLastRun >= 50ms) // far end is sending 2 datagrams every 100 ms
{
rcvSocket = OpenUdpSocket (&rcvAddr, localPort, rcvAddress, rcvNICAddress, rcvNICAddress); // in here I set the rcvSocket to Non-Blocking
sendSocket = OpenUdpSocket (&sendAddr, remotePort, sendAddress, sendNICAddress, sendNICAddress);
while (bytesRead = recvfrom (sendSocket, buff, maxBuffSize, sizeof (buff), 0, NULL, NULL)) > 0) // ALWAYS STUCK IN THIS LOOP
{
// decode data
}
// encode data
if ( sendto (sendSocket, buff, sizeoff(msg), 0, (struct sockaddr FAR *)&outSockaddr, sizeof (outSockaddr)) == 1) // NEVER GETS HERE !!
printf("sendto() error %d\n", WSAGetLastError());
closesocket(rcvSocket);
closesocket(sendSocket);
WSACleanup();
}
Any help including examples would be greatly appreciated.
RobL
08-08-2014 03:43 PM
Hey Rob_L2,
It seems like this is more of a question regarding the condition of your while loop. When would you expect it to evaluate to false so that it can reach the next function call?
I’m thinking that you may need to change your while loop architecture to allow the while loop condition to be met. There may be a possibility of using a producer/consumer form to try processing in another thread.
Hopefully this sparks an idea.
08-12-2014 11:58 AM
For whatever reason, the recvfrom in the while loop always returns a value greater than 0. I am reading the buffer 4 times faster than the sender is sending datagrams, but there is always data there. I would think that after I read the buffer, recvfrom would return 0 bytes. I changed it to an if statement and it works. I still do not know why recvfrom works this way or if there is actually always data in the buffer - very strange.
Thanks for the reply.
05-19-2021 01:57 AM
Hi Rob_L2,
I try to compile a project that include the recvfrom function, but it always give me a Undefined symbol '__imp_recvform error. Could you help to tell me which function panel fp should I added into the project?
05-19-2021 01:57 AM
Hi Rob_L2,
I try to compile a project that include the recvfrom function in the CVI, but it always give me a Undefined symbol '__imp_recvform error. Could you help to tell me which function panel fp should I added into the project?
05-19-2021 02:56 AM
Hi Jason,
I guess you need the Ws2_32.lib and winsock2.h from the Windows SDK.
Microsoft has a sample C++ program in the docs: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
05-24-2021 07:55 PM - edited 05-24-2021 07:55 PM
while (bytesRead = recvfrom (sendSocket, buff, maxBuffSize, sizeof (buff), 0, NULL, NULL)) > 0) // ALWAYS STUCK IN THIS LOOP
{
// decode data
}
I'm not sure if there was a copy paste error but something about those brackets definitely doesn't match up. I was suspecting operator precedence errors here and tried to reason about the bracket placement, but definitely get into a bind. You have 3 opening brackets followed by 4 closing brackets.