From 7032fedd41386f8a0b779d234620b473d978f889 Mon Sep 17 00:00:00 2001 From: wangzhengquan <wangzhengquan85@126.com> Date: 星期五, 17 七月 2020 17:43:18 +0800 Subject: [PATCH] req_rep finished --- test2/req_rep.c | 131 ++++++++++++++++++++++++++++++++----------- 1 files changed, 96 insertions(+), 35 deletions(-) diff --git a/test2/req_rep.c b/test2/req_rep.c index e2b4fd9..7fe0385 100644 --- a/test2/req_rep.c +++ b/test2/req_rep.c @@ -1,59 +1,120 @@ -#include "socket.h" +#include "mod_socket.h" +#include "shm_mm.h" +#include "usg_common.h" +typedef struct Targ { + int port; + int id; +}Targ; void server(int port) { - void *socket = shm_open_socket(REQ_REP); - shm_bind(socket, port); - shm_listen(socket); - int size; - void *recvbuf; - char sendbuf[512]; - while(true) { - shm_recv(socket, &recvbuf, &size); - sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf); - puts(sendbuf); - shm_send(socket, sendbuf, strlen(sendbuf)+1) ; - shm_free(recvbuf); - - } - shm_close_socket(socket); + void *socket = mod_open_socket(REQ_REP); + mod_socket_bind(socket, port); + mod_listen(socket); + int size; + void *recvbuf; + char sendbuf[512]; + while (mod_recv(socket, &recvbuf, &size) == 0) { + sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf); + puts(sendbuf); + mod_send(socket, sendbuf, strlen(sendbuf) + 1); + free(recvbuf); + } + mod_close_socket(socket); } void client(int port) { - void *socket = shm_open_socket(REQ_REP); - shm_connect(socket, port); + void *socket = mod_open_socket(REQ_REP); + mod_connect(socket, port); + int size; + void *recvbuf; + char sendbuf[512]; + while (true) { + printf("request: "); + scanf("%s", sendbuf); + mod_send(socket, sendbuf, strlen(sendbuf) + 1); + mod_recv(socket, &recvbuf, &size); + printf("reply: %s\n", (char *)recvbuf); + free(recvbuf); + } + mod_close_socket(socket); +} + + + + +void *threadrun(void *arg) { + Targ *targ = (Targ *)arg; + int port = targ->port; + char sendbuf[512]; + int scale = 100000; + int i; + void *socket = mod_open_socket(REQ_REP); + mod_connect(socket, port); + + char filename[512]; + sprintf(filename, "test%d.txt", targ->id); + FILE *fp = NULL; + fp = fopen(filename, "w+"); + int size; void *recvbuf; - char sendbuf[512]; - while(true) { - printf("request: "); - scanf("%s", sendbuf); - shm_send(socket, sendbuf, strlen(sendbuf)+1) ; - shm_recv(socket, &recvbuf, &size); - printf("reply: %s\n", (char *)recvbuf); - shm_free(recvbuf); + for (i = 0; i < scale; i++) { + sprintf(sendbuf, "thread(%d) %d", targ->id, i); - } - shm_close_socket(socket); + fprintf(fp, "requst:%s\n", sendbuf); + mod_send(socket, sendbuf, strlen(sendbuf)+1) ; + mod_recv(socket, &recvbuf, &size); + fprintf(fp, "reply: %s\n", (char *)recvbuf); + free(recvbuf); + } + fclose(fp); + mod_close_socket(socket); + return (void *)i; +} + +void multyThreadClient(int port) { + + int status, i = 0, processors = 4; + void *res[processors]; + Targ *targs = (Targ *)calloc(processors, sizeof(Targ)); + pthread_t tids[processors]; + char sendbuf[512]; + for (i = 0; i < processors; i++) { + targs[i].port = port; + targs[i].id = i; + pthread_create(&tids[i], NULL, threadrun, (void *)&targs[i]); + } + + for (i = 0; i < processors; i++) { + if (pthread_join(tids[i], &res[i]) != 0) { + perror("multyThreadClient pthread_join"); + } else { + fprintf(stderr, "client(%d) 鍐欏叆 %ld 鏉℃暟鎹甛n", i, (long)res[i]); + } + } } int main(int argc, char *argv[]) { shm_init(512); int port; if (argc < 3) { - fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client"); - return 1; + fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client"); + return 1; } port = atoi(argv[2]); - if (strcmp("server", argv[1]) == 0 ) { - server(port); + if (strcmp("server", argv[1]) == 0) { + server(port); } if (strcmp("client", argv[1]) == 0) - client(port); - shm_destroy(); - // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client"); + client(port); + + if (strcmp("mclient", argv[1]) == 0) + multyThreadClient(port); + shm_destroy(); + // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client"); return 0; } \ No newline at end of file -- Gitblit v1.8.0