From af85260254bacac40a68d4f5f61950523beb3a27 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期一, 19 十月 2020 17:02:41 +0800 Subject: [PATCH] update --- test_net_socket/net_mod_socket.c | 129 ++++++++++++++++++++++++++++++++++++++----- 1 files changed, 114 insertions(+), 15 deletions(-) diff --git a/test_net_socket/net_mod_socket.c b/test_net_socket/net_mod_socket.c index 873b225..a763c5e 100644 --- a/test_net_socket/net_mod_socket.c +++ b/test_net_socket/net_mod_socket.c @@ -1,34 +1,44 @@ -#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" +typedef struct Targ { + int port; + int id; + +}Targ; + void server(int port) { - NetModServerSocket *serverSocket = new NetModServerSocket(port); - serverSocket->start(); + void *serverSocket = net_mod_server_socket_open(port); + if(net_mod_server_socket_start(serverSocket) != 0) { + err_exit(errno, "net_mod_server_socket_start"); + } } void client(int port ){ - NetModSocket client; + void * client = net_mod_socket_open(); 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; + + net_mod_recv_msg_t *recv_arr; //192.168.20.104 net_node_t node_arr[] = { {"192.168.5.22", port, 11}, - {"192.168.20.10", port, 12}, - {"localhost", port, 13} + {"192.168.20.104", port, 21}, + {"192.168.20.104", port, 11} }; int pub_node_arr_size = 3; net_node_t pub_node_arr[] = { {"192.168.5.22", port, 8}, {"192.168.20.10", port, 8}, - {"localhost", port, 8} + {"192.168.20.104", port, 8} }; while (true) { @@ -40,8 +50,8 @@ 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); @@ -49,7 +59,8 @@ 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, @@ -58,8 +69,9 @@ 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) { @@ -70,9 +82,93 @@ } } - + net_mod_socket_close(client); +} + + +#define SCALE 100000 + +void *runclient(void *arg) { + Targ *targ = (Targ *)arg; + int port = targ->port; + char sendbuf[512]; + + int i,j, n, recv_arr_size; + net_mod_recv_msg_t *recv_arr; + + int node_arr_size = 1; + //192.168.20.104 + net_node_t node_arr[] = { + {NULL, port, 11} + }; + + void * client = net_mod_socket_open(); + + char filename[512]; + sprintf(filename, "test%d.tmp", targ->id); + FILE *fp = NULL; + fp = fopen(filename, "w+"); + // fp = stdout; + + int recvsize; + void *recvbuf; + for (i = 0; i < SCALE; i++) { + sprintf(sendbuf, "thread(%d) %d", targ->id, i); + fprintf(fp, "requst:%s\n", sendbuf); + n = net_mod_socket_sendandrecv(client, node_arr, node_arr_size, sendbuf, strlen(sendbuf) + 1, &recv_arr, &recv_arr_size); + //printf("send %d nodes\n", n); + for(j=0; j < recv_arr_size; j++) { + fprintf(fp, "reply: host:%s, port: %d, key:%d, content: %s\n", + recv_arr[j].host, + recv_arr[j].port, + recv_arr[j].key, + recv_arr[j].content + ); + } + // 浣跨敤瀹屽悗锛屼笉瑕佸繕璁伴噴鏀炬帀 + net_mod_socket_free_recv_msg_arr(recv_arr, recv_arr_size); + } + fclose(fp); + net_mod_socket_close(client); + return (void *)i; +} + +void mclient(int port) { + + int status, i = 0, processors = 4; + void *res[processors]; + // Targ *targs = (Targ *)calloc(processors, sizeof(Targ)); + Targ targs[processors]; + pthread_t tids[processors]; + char sendbuf[512]; + struct timeval start, end; + long total = 0; + + gettimeofday(&start, NULL); + for (i = 0; i < processors; i++) { + targs[i].port = port; + targs[i].id = i; + pthread_create(&tids[i], NULL, runclient, (void *)&targs[i]); + } + + for (i = 0; i < processors; i++) { + if (pthread_join(tids[i], &res[i]) != 0) { + perror("multyThreadClient pthread_join"); + } else { + total += (long)res[i]; + //fprintf(stderr, "client(%d) 鍐欏叆 %ld 鏉℃暟鎹甛n", i, (long)res[i]); + } + } + + gettimeofday(&end, NULL); + + double difftime = end.tv_sec * 1000000 + end.tv_usec - (start.tv_sec * 1000000 + start.tv_usec); + long diffsec = (long) (difftime/1000000); + long diffusec = difftime - diffsec*1000000; + fprintf(stderr,"鍙戦�佹暟鐩�: %ld, 鐢ㄦ椂: (%ld sec %ld usec), 骞冲潎: %f\n", total, diffsec, diffusec, difftime/total ); + // fflush(stdout); } int main(int argc, char *argv[]) { @@ -93,6 +189,9 @@ if (strcmp("client", argv[1]) == 0) client(port); + + if (strcmp("mclient", argv[1]) == 0) + mclient(port); } -- Gitblit v1.8.0