wangzhengquan
2020-06-11 5c9cbf5ea152f501ae976e0e0b4ef5ee98afdfba
service/client.c
@@ -6,6 +6,7 @@
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
const char *url = "tcp://127.0.0.1:8899";
const char *localUrl = "tcp://127.0.0.1:9988";
void
fatal(const char *func, int rv)
{
@@ -30,7 +31,7 @@
    std::string str = request.toStyledString();
    if ((rv = nng_send(sock, strdupa(str.c_str()), str.length(), 0)) != 0) {
    if ((rv = nng_send(sock, strdup(str.c_str()), str.length(), 0)) != 0) {
      fatal("nng_send", rv);
   }
   if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
@@ -119,10 +120,58 @@
}
int
server()
{
   nng_socket sock;
   int        rv;
   if ((rv = nng_rep0_open(&sock)) != 0) {
      fatal("nng_rep0_open", rv);
   }
   if ((rv = nng_listen(sock, localUrl, NULL, 0)) != 0) {
      fatal("nng_listen", rv);
   }
   for (;;) {
      char *   buf = NULL;
      size_t   sz;
      if ((rv = nng_recv(sock, &buf, &sz, NNG_FLAG_ALLOC)) != 0) {
         fatal("nng_recv", rv);
      }
      std::cout << buf << std::endl;
      Json::Value response;
      response["code"] = 0;
      std::string str = response.toStyledString();
      rv = nng_send(sock, strdup(str.c_str()), str.length(), NNG_FLAG_ALLOC);
      if (rv != 0) {
         fatal("nng_send", rv);
      }
      // if ((sz == sizeof(uint64_t)) &&
      //     ((GET64(buf, val)) == DATECMD)) {
      //    time_t now;
      //    printf("SERVER: RECEIVED DATE REQUEST\n");
      //    now = time(&now);
      //    printf("SERVER: SENDING DATE: ");
      //    showdate(now);
      //    // Reuse the buffer.  We know it is big enough.
      //    PUT64(buf, (uint64_t) now);
      //    continue;
      // }
      // Unrecognized command, so toss the buffer.
      //nng_free(buf, sz);
   }
}
int
main(const int argc, const char **argv)
{
   if ((argc > 1))
      return (client(argv[1]));
   if ((argc > 1)) {
     client(argv[1]);
     server();
   }
   // std::string str("123");
   // char *str2="123";
   // printf("str length  %d, %d\n", str.length(), strlen(str2));