/* ******************************************************** * TimeCastClnt.c: * * The original source code was developped by Bob Quinn * Modified for educational purpose by H. Fujinoki * * Released to the public domain * * Description: multicast client (receiver) application * * ******************************************************** */ #define _WINSOCK_DEPRECATED_NO_WARNINGS // for VS-2022 #define _CRT_SECURE_NO_WARNINGS // for VS-2022 #include #include #include #define BUFSIZE 1024 // the size of the outgoing buffer #define MAXADDRSTR 16 #define TIMECAST_ADDR "234.5.6.7" // Class-D (multicast) address #define TIMECAST_PORT 8910 // UDP port char achMCAddr[MAXADDRSTR] = TIMECAST_ADDR; // Class-D IP address u_short nPort = TIMECAST_PORT; // UDP port # u_long lMCAddr; // NIC-MAC address SYSTEMTIME *lpstSysTime, stSysTime; // for UTC timestamp void show_usage(void) { printf("usage: TimeCastClnt [multicast address][:port number]\n\n"); printf(" multicast address: 224.0.0.0 to 239.255.255.255\n"); printf(" port number: server's port number (16-bit)\n\n"); printf("If none provided, default values are %s:%d.\n", TIMECAST_ADDR, TIMECAST_PORT); } // MODULE MAIN //////////////////////////////////////////////////////////////// int main(int argc, char *argv[]) { int nRet; // result-status code int nIP_TTL = 2; // the scope of multicast transmisssion BOOL fFlag; // true/false flag char achInBuf [BUFSIZE]; // outgoing message buffer /* the variables/data-structures for WinSock ----------------------------- */ WSADATA stWSAData; // Winsock handler SOCKADDR_IN stLclAddr, stSrcAddr; // for "bind" struct ip_mreq stMreq; // multicast interface structure SOCKET hSocket; // socket handler /* display program banner ------------------------------------------------ */ printf("Multicast time client stated ...\n\n"); /* check the argument inputs (ignored if no argument --------------------- */ if ((argc > 2) || ((argc == 2) && (((strcmp(argv[1], "-help") == 0) || (strcmp(argv[1], "-?") == 0))))) { show_usage(); exit(1); } /* initalizing WinSock --------------------------------------------------- */ nRet = WSAStartup(0x0202, &stWSAData); if (nRet) { printf ("WSAStartup failed: %d\r\n", nRet); exit(1); } /* check input arguments ------------------------------------------------- */ if (argc == 2) { char *strTmp; strTmp = strchr(argv[1],':'); /* Get user port number (in host order) */ if (strTmp) { *strTmp = '\0'; nPort = atoi(++strTmp); if (nPort == 0) { printf("invalid port number provided by user.\n"); printf("using default port number: %d\n", TIMECAST_PORT); nPort = TIMECAST_PORT; } } /* get user multicast address */ if (argv[1][0] != '\0') { /* check the validity of the multicast address (224.0.0.0 0 239.255.255.255) */ lMCAddr = inet_addr(argv[1]); if (!((lMCAddr>=0xe0000000) || (lMCAddr<=0xefffffff))) { printf ("Invalid address %s provided by user.\n", argv[1]); printf ("using default address: %s\n", achMCAddr); } /* if Address is valid */ else { strcpy (achMCAddr, argv[1]); } } } /* construct a UDP (datagram) socket ------------------------------------- */ hSocket = socket(AF_INET, SOCK_DGRAM, 0); /* error check */ if (hSocket == INVALID_SOCKET) { printf ("socket() failed, Err: %d\n", WSAGetLastError()); WSACleanup(); exit(1); } /* Avoid (WSA)EADDRINUSE error on bind() (last one to bind should * receive dgrams sent to our port, but there are no guarantees of * this, unfortunately). */ fFlag = TRUE; // initialize the boolean flag /* set "reusable address" option to my socket (needed for assign a class-D address to NIC(s)) */ nRet = setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&fFlag, sizeof(fFlag)); if (nRet == SOCKET_ERROR) { printf ("setsockopt() SO_REUSEADDR failed, Err: %d\n", WSAGetLastError()); } /* set socket data structure (for bind) ---------------------------------- */ stLclAddr.sin_family = AF_INET; // IP stLclAddr.sin_addr.s_addr = htonl(INADDR_ANY); // to a class-D address stLclAddr.sin_port = htons(nPort); // UDP port /* bind the socket to an interface --------------------------------------- */ nRet = bind(hSocket, (struct sockaddr*) &stLclAddr, sizeof(stLclAddr)); if (nRet == SOCKET_ERROR) { printf ("bind() port: %d failed, Err: %d\n", nPort, WSAGetLastError()); } /* join the multicast group to receive datagram packets from the server -- */ stMreq.imr_multiaddr.s_addr = inet_addr(achMCAddr); // to Class-D address stMreq.imr_interface.s_addr = INADDR_ANY; // IP nRet = setsockopt(hSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)); if (nRet == SOCKET_ERROR) { printf( "setsockopt() IP_ADD_MEMBERSHIP address %s failed, Err: %d\n", achMCAddr, WSAGetLastError()); } /* starting message ------------------------------------------------------ */ printf("Now waiting for time updates from the TimeCast server\n"); printf(" multicast group address: %s, port number: %d\n", achMCAddr, nPort); lpstSysTime = (SYSTEMTIME *)achInBuf; // obtain the current time /* the main infinite-loop ------------------------------------------------ */ while (1) { int addr_size = sizeof(struct sockaddr_in); /* receive a multicast datgram (UDP) packet from the sender --- */ nRet = recvfrom(hSocket, achInBuf, BUFSIZE, 0, (struct sockaddr*)&stSrcAddr, &addr_size); if (nRet < 0) { printf ("recvfrom() failed, Error: %d\n", WSAGetLastError()); WSACleanup(); exit(1); // terminate the client process on error } /* display the received message ------------------------------- */ printf( "UTC Time %02d:%02d:%02d:%03d on %02d-%02d-%d from %s:%d\n", lpstSysTime->wHour, lpstSysTime->wMinute, lpstSysTime->wSecond, lpstSysTime->wMilliseconds, lpstSysTime->wMonth, lpstSysTime->wDay, lpstSysTime->wYear, inet_ntoa(stSrcAddr.sin_addr), ntohs(stSrcAddr.sin_port)); /* Set the local time using the system (UTC) time from server */ nRet = SetSystemTime(lpstSysTime); nRet = 1; // for testing if (!nRet) { // printf ("SetLocalTime() failed, Err: %d\n", // GetLastError()); } else { GetLocalTime(&stSysTime); // printf ( // " Local Time reset to %02d-%02d-%02d at %02d:%02d:%02d:%03d\n", // stSysTime.wMonth, stSysTime.wDay, stSysTime.wYear, // stSysTime.wHour, stSysTime.wMinute, // stSysTime.wSecond, stSysTime.wMilliseconds); } } /* end of the main while-loop ------------------------------------------ */ /* Leave the multicast group: this part will not be executed but just for a reference for standard mutlicast processes */ stMreq.imr_multiaddr.s_addr = inet_addr(achMCAddr); stMreq.imr_interface.s_addr = INADDR_ANY; nRet = setsockopt(hSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char *)&stMreq, sizeof(stMreq)); if (nRet == SOCKET_ERROR) { printf ( "setsockopt() IP_DROP_MEMBERSHIP address %s failed, Err: %d\n", achMCAddr, WSAGetLastError()); } /* Close the socket */ closesocket(hSocket); /* Tell WinSock we're leaving */ WSACleanup(); } // END OF MODULE MAIN /////////////////////////////////////////////////////////