#include "net_mod_server_socket.h" #include "net_mod_socket.h" #include "shm_mm.h" #include "dgram_mod_socket.h" #include "usg_common.h" void server(int port) { NetModServerSocket *serverSocket = new NetModServerSocket(port); serverSocket->start(); } void client(int port ){ NetModSocket client; char content[MAXLINE]; char action[512]; char topic[512]; net_mod_recv_msg_t *recv_arr; int recv_arr_size, i, n; int node_arr_size = 3; //192.168.20.104 net_node_t node_arr[] = { {"192.168.20.104", port, 11}, {"192.168.20.104", port, 12}, {"192.168.20.104", port, 13} }; 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 [content] or sub \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) { // 收到消息的节点即使没有对应的信息, 也要回复一个表示无的消息,否则会一直等待 n = client.sendandrecv( node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size); for(i=0; i \n", argv[0], "server", "client"); return 1; } port = atoi(argv[2]); if (strcmp("server", argv[1]) == 0 ) { server(port); } if (strcmp("client", argv[1]) == 0) client(port); }