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