Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Head request example
- * View with tabsize = 4
- * Part of this is from the Winsock networking tutorial by Thomas Bleeker
- * Visit www.MadWizard.org
- */
- #include <iostream>
- #pragma warning(disable:4996)
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define WIN32_MEAN_AND_LEAN
- #include <winsock2.h>
- #include <windows.h>
- #include <time.h>
- using namespace std;
- class HRException
- {
- public:
- HRException() :
- m_pMessage("") {}
- virtual ~HRException() {}
- HRException(const char *pMessage) :
- m_pMessage(pMessage) {}
- const char * what() { return m_pMessage; }
- private:
- const char *m_pMessage;
- };
- const int REQ_WINSOCK_VER = 2; // Minimum winsock version required
- const char DEF_SERVER_NAME[] = "0.pl.pool.ntp.org";
- const int SERVER_PORT = 123;
- const int TEMP_BUFFER_SIZE = 1024;
- const char msg[48] = { 010,0,0,0,0,0,0,0,0 };
- // IP number typedef for IPv4
- typedef unsigned long IPNumber;
- IPNumber FindHostIP(const char *pServerName)
- {
- HOSTENT *pHostent;
- // Get hostent structure for hostname:
- if (!(pHostent = gethostbyname(pServerName)))
- throw HRException("could not resolve hostname.");
- // Extract primary IP address from hostent structure:
- if (pHostent->h_addr_list && pHostent->h_addr_list[0])
- return *reinterpret_cast<IPNumber*>(pHostent->h_addr_list[0]);
- return 0;
- }
- void FillSockAddr(sockaddr_in *pSockAddr, const char *pServerName, int portNumber)
- {
- // Set family, port and find IP
- pSockAddr->sin_family = AF_INET;
- pSockAddr->sin_port = htons(portNumber);
- pSockAddr->sin_addr.S_un.S_addr = FindHostIP(pServerName);
- }
- bool RequestHeaders(const char *pServername)
- {
- SOCKET hSocket = INVALID_SOCKET;
- char tempBuffer [TEMP_BUFFER_SIZE];
- sockaddr_in sockAddr = { 0 };
- bool bSuccess = true;
- try
- {
- // Lookup hostname and fill sockaddr_in structure:
- cout << "Looking up hostname " << pServername << "... ";
- FillSockAddr(&sockAddr, pServername, SERVER_PORT);
- cout << "found.\n";
- // Create socket
- cout << "Creating socket... ";
- if ((hSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
- throw HRException("could not create socket.");
- cout << "created.\n";
- // Connect to server
- cout << "Attempting to connect to " << inet_ntoa(sockAddr.sin_addr)
- << ":" << SERVER_PORT << "... ";
- if (connect(hSocket, reinterpret_cast<sockaddr*>(&sockAddr), sizeof(sockAddr)) != 0)
- throw HRException("could not connect.");
- cout << "connected.\n";
- cout << "Sending request... ";
- // send request part 1
- if (send(hSocket, msg, sizeof(msg) , 0) == SOCKET_ERROR)
- throw HRException("failed to send data.");
- cout << "request sent.\n";
- cout << "Dumping received data...\n\n";
- time_t tmit;
- int bytes = recv(hSocket, tempBuffer, sizeof(tempBuffer), 0); // <-- the problem
- cout << "bytes received: " << bytes << endl;
- tmit = ntohl(((time_t*)tempBuffer)[4]);
- tmit -= 2208988800U;
- time_t now = time(0);
- cout<<"NTP time: " << ctime(&tmit);
- cout << "Pc time: " << ctime(&now);
- time_t difference = tmit - now;
- if (difference != 0)
- {
- cout << "Your system time is " << (difference < 0 ? -difference : difference)
- << (difference > 0 ? " seconds slower" : " seconds faster") << " than it should" << endl;
- }
- else
- cout << "Your system time is fine!\n";
- }
- catch (HRException e)
- {
- cerr << "\nError: " << e.what() << endl;
- bSuccess = false;
- }
- if (hSocket != INVALID_SOCKET)
- {
- closesocket(hSocket);
- }
- return bSuccess;
- }
- int main(int argc, char* argv[])
- {
- int iRet = 1;
- WSADATA wsaData;
- cout << "Initializing winsock... ";
- if (WSAStartup(MAKEWORD(REQ_WINSOCK_VER, 0), &wsaData) == 0)
- {
- // Check if major version is at least REQ_WINSOCK_VER
- if (LOBYTE(wsaData.wVersion) >= REQ_WINSOCK_VER)
- {
- cout << "initialized.\n";
- // Set default hostname:
- const char *pHostname = DEF_SERVER_NAME;
- // Set custom hostname if given on the commandline:
- if (argc > 1)
- pHostname = argv[1];
- iRet = !RequestHeaders(pHostname);
- }
- else
- {
- cerr << "required version not supported!";
- }
- cout << "Cleaning up winsock... ";
- // Cleanup winsock
- if (WSACleanup() != 0)
- {
- cerr << "cleanup failed!\n";
- iRet = 1;
- }
- cout << "done.\n";
- }
- else
- {
- cerr << "startup failed!\n";
- }
- int x;
- cin >> x;
- return iRet;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement