#include "mod_socket.h"
|
#include "shm_mm_wraper.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_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);
|
|
shm_mm_wrapper_destroy();
|
// fprintf(stderr, "Usage: reqrep %s|%s <URL> ...\n", "server", "client");
|
return 0;
|
}
|