#include "dgram_mod_socket.h" #include "shm_mm_wraper.h" #include "usg_common.h" void server(int port) { void *socket = dgram_mod_open_socket(); dgram_mod_bind(socket, port); int size; void *recvbuf; char sendbuf[512]; int rv; int remote_port; if ( (rv = dgram_mod_recvfrom_timeout(socket, &recvbuf, &size, &remote_port, 5, 0) ) == 0) { printf( "RECEIVED HREARTBEAT FROM %d: %s\n", remote_port, recvbuf); free(recvbuf); } else { printf("RECEIVED failture, timeout\n"); } sleep(100); dgram_mod_close_socket(socket); } void client(int port) { void *socket = dgram_mod_open_socket(); int size; char sendbuf[512]; long i = 0; while (true) { sprintf(sendbuf, "%d", i); if(dgram_mod_sendto_timeout(socket, sendbuf, strlen(sendbuf) + 1, port, 2, 0) == 0) { printf("SEND HEART:%s\n", sendbuf); printf("send success\n"); } else { printf("send failture, timeout\n"); } i++; } dgram_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; }