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
| #include "shm_socket.h"
| #include "usg_common.h"
| #include "shm_mm.h"
|
| #include "shm_socket.h"
| #include "usg_common.h"
| #include "shm_mm.h"
| typedef struct Targ {
| int port;
| int id;
| }Targ;
|
|
| void server(int port) {
| pthread_t tid;
| shm_socket_t *socket = shm_open_socket(SHM_SOCKET_DGRAM);
| shm_socket_bind(socket, port);
|
| char *buf;
| int size;
| int remotePort;
| char sendbuf[512];
| while( shm_recvfrom(socket, (void **)&buf, &size, &remotePort) == 0) {
| sprintf(sendbuf, "RECEIVED:%s", buf);
| printf("received from %d:%s\n", remotePort, buf);
| shm_sendto(socket, (void *)sendbuf, strlen(sendbuf) + 1, remotePort);
| free(buf);
| }
|
| shm_close_socket(socket);
|
| }
|
| void client(int port) {
| shm_socket_t *socket = shm_open_socket(SHM_SOCKET_DGRAM);
| int size;
| char *recvbuf;
| char sendbuf[512];
| int remote_port;
| while(true) {
| printf("request: ");
| scanf("%s", sendbuf);
| shm_sendto(socket, sendbuf, strlen(sendbuf)+1, port) ;
| shm_recvfrom(socket, (void **)&recvbuf, &size, &remote_port);
| printf("reply from (%d): %s\n", remote_port, recvbuf);
| free(recvbuf);
|
| }
| shm_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;
| }
|
|