Reputation: 19
I am using IPG CarMaker and am trying to export the data to another machine using UDP, real time. I am referring to http://www.codeproject.com/Articles/11740/A-simple-UDP-time-server-and-client-for-beginners. I want to send the position of the car whenever the client requests it. Now I have included the above code into CarMakers main loop which has a timestep of 1 ms. There is no problem while I build the program (using Visual Studio 2010). But when I try running simulations in CarMaker, the following line causes the simulation to get timed out:
bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
As a result my simulation does not start at all! Is it because I don't have a client running alongside at the same time? Please help! The code in the main loop is as follows:
User_DrivMan_Calc (double dt) { client_length = (int)sizeof(struct sockaddr_in);
bytes_received = recvfrom(sd, buffer, BUFFER_SIZE, 0, (struct sockaddr *)&client, &client_length);
if (bytes_received < 0)
{
fprintf(f, "Could not receive datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
/* Check for time request */
if (strcmp(buffer, "GET DISTANCE\r\n") == 0)
{
/* Get current time */
current_time = time(NULL);
d = Car.Distance;
/* Send data back */
if (sendto(sd, (char *)&d, (int)sizeof(d), 0, (struct sockaddr *)&client, client_length) != (int)sizeof(d))
{
fprintf(f, "Error sending datagram.\n");
closesocket(sd);
WSACleanup();
exit(0);
}
}
return 0;
}
Upvotes: 0
Views: 284
Reputation: 399713
That is a blocking read, i.e. it won't return from the function until the read has succeeded.
If no data is waiting, it will block for a very long time.
You need to use e.g. select()
to detect when data is available, or make the socket non-blocking.
Also, if your packet rate is going to be 1,000 Hz, that is (way) too fast for this kind of application, in general.
Upvotes: 0