| | |
| | | exit(0); |
| | | } |
| | | |
| | | void server(int port, bool restart) { |
| | | void server(int key, bool restart) { |
| | | server_socket = dgram_mod_open_socket(); |
| | | |
| | | |
| | | if(restart) { |
| | | dgram_mod_force_bind(server_socket, port); |
| | | dgram_mod_force_bind(server_socket, key); |
| | | } else { |
| | | dgram_mod_bind(server_socket, port); |
| | | dgram_mod_bind(server_socket, key); |
| | | } |
| | | |
| | | |
| | |
| | | pthread_detach(pthread_self()); |
| | | void *recvbuf; |
| | | int size; |
| | | int port; |
| | | while (dgram_mod_recvfrom( socket, &recvbuf, &size, &port) == 0) { |
| | | int key; |
| | | while (dgram_mod_recvfrom( socket, &recvbuf, &size, &key) == 0) { |
| | | printf("收到订阅消息:%s\n", recvbuf); |
| | | free(recvbuf); |
| | | } |
| | | |
| | | } |
| | | |
| | | void client(int port) { |
| | | void client(int key) { |
| | | void *socket = dgram_mod_open_socket(); |
| | | |
| | | |
| | | pthread_t tid; |
| | | pthread_create(&tid, NULL, run_recv, socket); |
| | | int size; |
| | |
| | | if(strcmp(action, "sub") == 0) { |
| | | printf("Please input topic!\n"); |
| | | scanf("%s", topic); |
| | | if (dgram_mod_sub(socket, topic, strlen(topic), port) == 0) { |
| | | if (dgram_mod_sub(socket, topic, strlen(topic), key) == 0) { |
| | | printf("%d Sub success!\n", dgram_mod_get_port(socket)); |
| | | } else { |
| | | printf("Sub failture!\n"); |
| | |
| | | } else if(strcmp(action, "desub") == 0) { |
| | | printf("Please input topic!\n"); |
| | | scanf("%s", topic); |
| | | if (dgram_mod_desub(socket, topic, strlen(topic), port) == 0) { |
| | | if (dgram_mod_desub(socket, topic, strlen(topic), key) == 0) { |
| | | printf("%d Desub success!\n", dgram_mod_get_port(socket)); |
| | | } else { |
| | | printf("Desub failture!\n"); |
| | |
| | | // printf("%s %s %s\n", action, topic, content); |
| | | printf("Please input topic and content\n"); |
| | | scanf("%s %s", topic, content); |
| | | if(dgram_mod_pub(socket, topic, strlen(topic)+1, content, strlen(content)+1, port) == 0){ |
| | | if(dgram_mod_pub(socket, topic, strlen(topic)+1, content, strlen(content)+1, key) == 0){ |
| | | printf("%d Pub success!\n", dgram_mod_get_port(socket)); |
| | | } else { |
| | | printf("Pub failture!\n"); |
| | |
| | | |
| | | int main(int argc, char *argv[]) { |
| | | shm_init(512); |
| | | int port; |
| | | int key; |
| | | if (argc < 3) { |
| | | fprintf(stderr, "Usage: %s %s|%s|rmkey <PORT> ...\n", argv[0], "server", "client"); |
| | | fprintf(stderr, "Usage: %s %s|%s|rmkey <key> ...\n", argv[0], "server", "client"); |
| | | return 1; |
| | | } |
| | | |
| | | port = atoi(argv[2]); |
| | | key = atoi(argv[2]); |
| | | |
| | | if (strcmp("server", argv[1]) == 0) { |
| | | if(argc >= 4 && strcmp("restart", argv[3]) == 0) { |
| | | server(port, true); |
| | | server(key, true); |
| | | } |
| | | else{ |
| | | server(port, false); |
| | | server(key, false); |
| | | } |
| | | |
| | | } else if (strcmp("client", argv[1]) == 0) { |
| | | client(port); |
| | | client(key); |
| | | } else if(strcmp("rmkey", argv[1]) == 0) { |
| | | for(int i = 2; i < argc; i++) { |
| | | port = atoi(argv[i]); |
| | | dgram_mod_remove_key(port); |
| | | // printf("%d\n", port); |
| | | key = atoi(argv[i]); |
| | | dgram_mod_remove_key(key); |
| | | // printf("%d\n", key); |
| | | } |
| | | } |
| | | |