From 63596b63037b5036772d9152ad959e1049937bbf Mon Sep 17 00:00:00 2001
From: wangzhengquan <wangzhengquan85@126.com>
Date: 星期二, 22 十二月 2020 10:59:09 +0800
Subject: [PATCH] print which one close
---
test_socket/dgram_mod_survey.c | 101 +++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 86 insertions(+), 15 deletions(-)
diff --git a/test_socket/dgram_mod_survey.c b/test_socket/dgram_mod_survey.c
index 988877b..da3260f 100644
--- a/test_socket/dgram_mod_survey.c
+++ b/test_socket/dgram_mod_survey.c
@@ -1,41 +1,112 @@
#include "dgram_mod_socket.h"
-#include "shm_mm.h"
+#include "shm_mm_wraper.h"
#include "usg_common.h"
+
+typedef struct Targ {
+ int port;
+ int id;
+
+}Targ;
+
+void sigint_handler(int sig) {
+ //dgram_mod_close_socket(server_socket);
+ printf("===Catch sigint======================\n");
+ shm_mm_wrapper_destroy();
+ exit(0);
+}
+
void server(int port) {
- void *socket = dgram_mod_open_socket(SURVEY);
+ void *socket = dgram_mod_open_socket();
dgram_mod_bind(socket, port);
int size;
void *recvbuf;
char sendbuf[512];
int rv;
int remote_port;
- while ( (rv = dgram_mod_recvfrom(socket, &recvbuf, &size, &remote_port) ) == 0) {
- printf( "鏀跺埌鏉ヨ嚜%d娉ㄥ唽淇℃伅: %s", remote_port, recvbuf);
- free(recvbuf);
+ while (true) {
+ if ((rv = dgram_mod_recvfrom_timeout(socket, &recvbuf, &size, &remote_port, 15, 0) ) == 0) {
+ printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
+ free(recvbuf);
+ }
+
}
dgram_mod_close_socket(socket);
}
void client(int port) {
- void *socket = dgram_mod_open_socket(SURVEY);
+ void *socket = dgram_mod_open_socket();
int size;
- void *recvbuf;
char sendbuf[512];
+ long i = 0;
while (true) {
- printf("request: ");
- scanf("%s", sendbuf);
- dgram_mod_send(socket, sendbuf, strlen(sendbuf) + 1, port, &recvbuf, &size);
- printf("reply: %s\n", (char *)recvbuf);
- free(recvbuf);
+ sprintf(sendbuf, "%d", i);
+ printf("SEND HEART:%s\n", sendbuf);
+ dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port);
+ // sleep(1);
+ i++;
}
dgram_mod_close_socket(socket);
}
+
+void *runclient(void *arg) {
+ signal(SIGINT, sigint_handler);
+ Targ *targ = (Targ *)arg;
+ int port = targ->port;
+ void *socket = dgram_mod_open_socket();
+ int size;
+ char sendbuf[512];
+ long scale = 10;
+ long i = 0;
+ while (i < scale) {
+ sprintf(sendbuf, "%d", i);
+ printf("%d SEND HEART:%s\n", targ->id, sendbuf);
+ dgram_mod_sendto(socket, sendbuf, strlen(sendbuf) + 1, port);
+ sleep(1);
+ i++;
+ }
+
+ dgram_mod_close_socket(socket);
+ return (void *)i;
+}
+
+void startClients(int port) {
+
+ int status, i = 0, processors = 100;
+ void *res[processors];
+ Targ *targs = (Targ *)calloc(processors, sizeof(Targ));
+ pthread_t tids[processors];
+ char sendbuf[512];
+
+ struct timeval start;
+ 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 {
+ fprintf(stderr, "client(%d) 鍙戦�� %ld 鏉℃暟鎹甛n", i, (long)res[i]);
+ }
+ }
+
+ struct timeval end;
+ 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 diffmsec = difftime - diffsec*1000000;
+ printf("cost: %ld sec: %ld msc\n", diffsec, diffmsec);
+}
int main(int argc, char *argv[]) {
- shm_init(512);
+ shm_mm_wrapper_init(512);
int port;
if (argc < 3) {
fprintf(stderr, "Usage: reqrep %s|%s <PORT> ...\n", "server", "client");
@@ -51,6 +122,6 @@
if (strcmp("client", argv[1]) == 0)
client(port);
-
+ shm_mm_wrapper_destroy();
return 0;
-}
\ No newline at end of file
+}
--
Gitblit v1.8.0