int server (const char *url)
{
int sock = nn_socket (AF_SP, NN_SURVEYOR);
assert (sock >= 0);
assert (nn_bind (sock, url) >= 0);
sleep(1); // wait for connections
int sz_d = strlen(DATE) + 1; //'\0' too
printf ("SERVER: SENDING DATE SURVEY REQUEST\n");
int bytes = nn_send (sock, DATE, sz_d, 0);
assert (bytes == sz_d);
while (1)
{
char *buf = NULL;
int bytes = nn_recv (sock, &buf, NN_MSG, 0);
if (bytes == ETIMEDOUT) break;
if (bytes >= 0)
{
printf ("SERVER: RECEIVED \ "%s\" SURVEY RESPONSE\n", buf);
nn_freemsg (buf);
}
}
return nn_shutdown (sock, 0);
}
Because the socket type is NN_SURVEY OR, so the while loop will not wait at nn_recv. For the client, the socket type is NN_RESPONDENT, so the loop in the client waits for nn_recv.
Now, because the while loop runs continuously indefinitely, the CPU The utilization rate is as high as 99%. Can you tell me that there are other ways to use nanomsg for survey structure.
I am using IPC’s nanomsg usage and want to use the SURVEY-archetype described here Architecture. In this process, the process runs as a client and a server and exchanges data. Now the server has the following code (also listed in the link provided):
< pre>int server (const char *url)
{
int sock = nn_socket (AF_SP, NN_SURVEYOR);
assert (sock >= 0);
assert (nn_bind (sock , url) >= 0);
sleep(1); // wait for connections
int sz_d = strlen(DATE) + 1; //’\0′ too
printf (” SERVER: SENDING DATE SURVEY REQUEST\n”);
int bytes = nn_send (sock, DATE, sz_d, 0);
assert (bytes == sz_d);
while (1)< br /> {
char *buf = NULL;
int bytes = nn_recv (sock, &buf, NN_MSG, 0);
if (bytes == ETIMEDOUT) break;
i f (bytes >= 0)
{
printf (“SERVER: RECEIVED \”%s\” SURVEY RESPONSE\n”, buf);
nn_freemsg (buf);
}
}
return nn_shutdown (sock, 0);
)
Since the socket type is NN_SURVEYOR, the while loop will not wait at nn_recv. For customers At the end, the socket type is NN_RESPONDENT, so the loop in the client waits for nn_recv.
Now, because the while loop runs continuously indefinitely, the CPU usage rate is as high as 99%. Can you tell me, and Other methods can use nanomsg to investigate the architecture.
If you don’t receive anything, try adding a yield() call at the end of the loop.