wangzhengquan
2020-09-25 00dba6082e245d917cb7d6eed3c627211ff41cd7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "dgram_mod_socket.h"
#include "shm_mm.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_destroy();
  exit(0);
}
 
void server(int port) {
  void *socket = dgram_mod_open_socket();
  dgram_mod_bind(socket, port);
  int size;
  void *recvbuf;
  char sendbuf[512];
  int rv;
  int remote_port;
  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();
  int size;
  char sendbuf[512];
  long i = 0;
  while (true) {
    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);
  int port;
  if (argc < 3) {
    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("client", argv[1]) == 0)
    client(port);
 
  shm_destroy();
  return 0;
}