| | |
| | | #include "net_mod_server_socket.h" |
| | | #include "net_mod_socket.h" |
| | | #include "net_mod_server_socket_wrapper.h" |
| | | #include "net_mod_socket_wrapper.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 *serverSocket = net_mod_server_socket_open(port); |
| | | net_mod_server_socket_start(serverSocket); |
| | | } |
| | | |
| | | void client(int port ){ |
| | | NetModSocket client; |
| | | void * client = net_mod_socket_open(); |
| | | char content[MAXLINE]; |
| | | char action[512]; |
| | | char topic[512]; |
| | |
| | | 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); |
| | | n = net_mod_socket_pub(client, pub_node_arr, pub_node_arr_size, topic, strlen(topic)+1, content, strlen(content)+1); |
| | | printf("pub %d nodes\n", n); |
| | | } |
| | | else if(strcmp(action, "send") == 0) { |
| | | getc(stdin); |
| | |
| | | |
| | | if (fgets(content, MAXLINE, stdin) != NULL) { |
| | | // 收到消息的节点即使没有对应的信息, 也要回复一个表示无的消息,否则会一直等待 |
| | | n = client.sendandrecv( node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size); |
| | | n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, content, strlen(content), &recv_arr, &recv_arr_size); |
| | | printf("send %d nodes\n", n); |
| | | for(i=0; i<recv_arr_size; i++) { |
| | | printf("host:%s, port: %d, key:%d, content: %s\n", |
| | | recv_arr[i].host, |
| | |
| | | recv_arr[i].content |
| | | ); |
| | | } |
| | | //使用完后,不要忘记释放掉 |
| | | NetModSocket::free_recv_msg_arr(recv_arr, recv_arr_size); |
| | | |
| | | // 使用完后,不要忘记释放掉 |
| | | net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); |
| | | } |
| | | } |
| | | else if(strcmp(action, "quit") == 0) { |
| | |
| | | } |
| | | |
| | | } |
| | | |
| | | net_mod_socket_close(client); |
| | | |
| | | |
| | | } |