Created
May 19, 2017 14:20
-
-
Save anton-povarov/22971249e311f5382feddee6e918d22d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdio.h> | |
#include <pthread.h> | |
#include <nanomsg/nn.h> | |
#include <nanomsg/reqrep.h> | |
static char* endpoint = "inproc://test"; | |
void* thread_func(void *ctx) | |
{ | |
int sock = nn_socket(AF_SP, NN_REP); | |
assert(sock >= 0); | |
int r = nn_bind(sock, endpoint); | |
assert(r >= 0); | |
while (1) | |
{ | |
int value; | |
int n; | |
n = nn_recv(sock, &value, sizeof(value), 0); | |
if (n != sizeof(value)) | |
break; | |
fprintf(stderr, "got value: %d\n", value); | |
double d = value; | |
n = nn_send(sock, (void*)&d, sizeof(d), 0); | |
assert(n == sizeof(d)); | |
} | |
return NULL; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
pthread_t t; | |
int r = pthread_create(&t, NULL, thread_func, NULL); | |
if (r != 0) | |
{ | |
fprintf(stderr, "pthread_create failed, %d: %s\n", r, strerror(r)); | |
return 1; | |
} | |
while (1) | |
{ | |
fprintf(stderr, "press for next iteration, 'c' to stop\n"); | |
int c = getchar(); | |
if (c == 'c') | |
break; | |
int sock = nn_socket(AF_SP, NN_REQ); | |
assert(sock >= 0); | |
int r; | |
r = nn_connect(sock, endpoint); | |
assert(r >= 0); | |
int value = 1; | |
r = nn_send(sock, &value, sizeof(value), 0); | |
assert(r == sizeof(value)); | |
double d; | |
r = nn_recv(sock, &d, sizeof(d), 0); | |
assert(r == sizeof(d)); | |
fprintf(stderr, "response: %lf\n", d); | |
r = nn_close(sock); | |
assert(r >= 0); | |
} | |
/* calling this will force nanomsg to free everything, finally */ | |
nn_term(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment