fujuntang
2021-08-11 68d23225a38a35f1325eb39fa4ed5a005d5de473
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
#include "dgram_mod_socket.h"
#include "shm_mm_wraper.h"
#include "usg_common.h"
 
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;
  if ( (rv = dgram_mod_recvfrom_timeout(socket, &recvbuf, &size, &remote_port, 5, 0) ) == 0) {
    printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf);
    free(recvbuf);
  } else {
    printf("RECEIVED failture, timeout\n");
  }
  sleep(100);
  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);
   
    if(dgram_mod_sendto_timeout(socket, sendbuf, strlen(sendbuf) + 1, port, 2, 0) == 0) {
       printf("SEND HEART:%s\n", sendbuf);
       printf("send success\n");
    } else {
       printf("send failture, timeout\n");
    }
    i++;
  }
  dgram_mod_close_socket(socket);
}
 
 
 
int main(int argc, char *argv[]) {
  shm_mm_wrapper_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);
 
  
  return 0;
}