#include "mod_socket.h" #include "shm_mm_wraper.h" #include "usg_common.h" void server(int port) { void *socket = mod_open_socket(REQ_REP); mod_socket_bind(socket, port); mod_listen(socket); int size; void *recvbuf; char sendbuf[512]; int rv; while ( (rv = mod_recv(socket, &recvbuf, &size) ) == 0) { sprintf(sendbuf, "SERVER RECEIVED: %s", recvbuf); puts(sendbuf); mod_send(socket, sendbuf, strlen(sendbuf) + 1); free(recvbuf); } mod_close_socket(socket); } void client(int port) { void *socket = mod_open_socket(REQ_REP); mod_connect(socket, port); int size; void *recvbuf; char sendbuf[512]; while (true) { printf("request: "); scanf("%s", sendbuf); mod_send(socket, sendbuf, strlen(sendbuf) + 1); mod_recv(socket, &recvbuf, &size); printf("reply: %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 ...\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; }