wangzhengquan
2020-10-13 2a4e4619f34a742e36693e589e0431347a72979b
test_net_socket/net_mod_req_rep.c
@@ -11,19 +11,45 @@
void client(int port ){
   NetModSocket client;
   char send_buf[MAXLINE];
   char content[MAXLINE];
   char action[512];
  char topic[512];
   net_mod_recv_msg_t *recv_arr;
   int recv_arr_size, i;
   int recv_arr_size, i, n;
   int node_arr_size = 3;
   //192.168.20.104
   net_node_t node_arr[] = {
      {"localhost", port, 11},
      {"localhost", port, 12},
      {"localhost", port, 13},
      {"localhost", port, 14}
      {"192.168.20.104", port, 11},
      {"192.168.20.104", port, 12},
      {"192.168.20.104", port, 13}
   };
  while (fgets(send_buf, MAXLINE, stdin) != NULL) {
   int pub_node_arr_size = 3;
   net_node_t pub_node_arr[] = {
      {"192.168.20.104", port, 8},
      {"192.168.20.104", port, 8},
      {"192.168.20.104", port, 8}
   };
  while (true) {
    //printf("Usage: pub <topic> [content] or sub <topic>\n");
    printf("Can I help you? pub, send or quit\n");
    scanf("%s",action);
    if(strcmp(action, "pub") == 0) {
       printf("Please input topic and content\n");
      scanf("%s %s", topic, content);
       n = client.pub(pub_node_arr, pub_node_arr_size, topic, strlen(topic)+1, content, strlen(content)+1);
       printf("pub %d\n", n);
    }
    else if(strcmp(action, "send") == 0) {
       getc(stdin);
       printf("Please input  content\n");
        if (fgets(content, MAXLINE, stdin) != NULL) {
     // 收到消息的节点即使没有对应的信息, 也要回复一个表示无的消息,否则会一直等待
    client.sendandrecv( node_arr, 4, send_buf, strlen(send_buf), &recv_arr, &recv_arr_size);
          n = client.sendandrecv( node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size);
    for(i=0; i<recv_arr_size; i++) {
       printf("host:%s, port: %d, key:%d, content: %s\n", 
          recv_arr[i].host,
@@ -36,6 +62,18 @@
    NetModSocket::free_recv_msg_arr(recv_arr, recv_arr_size);
  }
}
    else if(strcmp(action, "quit") == 0) {
      break;
    } else {
      printf("error input argument\n");
      continue;
    }
  }
}
int main(int argc, char *argv[]) {
   shm_init(512);