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
| #include "mod_socket.h"
| #include "shm_mm.h"
| #include "usg_common.h"
|
|
| void server(int port) {
| void *socket = mod_open_socket(PUB_SUB);
| mod_socket_bind(socket, port);
| mod_listen(socket);
| int size;
| void *recvbuf;
| char sendbuf[512];
| while(true) {
| printf("请输入发布消息:");
| scanf("%s", sendbuf);
| mod_send(socket, sendbuf, strlen(sendbuf)+1) ;
| free(recvbuf);
|
| }
| mod_close_socket(socket);
| }
|
| void client(int port) {
| void *socket = mod_open_socket(PUB_SUB);
| mod_connect(socket, port);
| int size;
| void *recvbuf;
|
| while(mod_recv(socket, &recvbuf, &size) == 0) {
| printf("收到订阅消息: %s\n", (char *)recvbuf);
| free(recvbuf);
|
| }
| mod_close_socket(socket);
| }
|
| 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();
| // fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client");
| return 0;
| }
|
|