LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

UDP recvfrom

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

 

0 Kudos
Message 1 of 7
(6,376 Views)

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.

Taylor
Message 2 of 7
(6,350 Views)

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.

0 Kudos
Message 3 of 7
(6,306 Views)

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?

0 Kudos
Message 4 of 7
(2,758 Views)

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?

0 Kudos
Message 5 of 7
(2,757 Views)

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

 

-----------------------
/* Nothing past this point should fail if the code is working as intended */
Message 6 of 7
(2,750 Views)

 

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.

Rolf Kalbermatter
My Blog
0 Kudos
Message 7 of 7
(2,706 Views)