📄 clientmain.cpp
字号:
#include <sys/stat.h>#include <fcntl.h>#include <errno.h>#include <netdb.h>#include <time.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <signal.h>#include <iostream>typedef int SOCKLEN;typedef unsigned long DWORD;typedef unsigned long long ULONGLONG;typedef struct _FILETIME{ DWORD dwLowDateTime; DWORD dwHighDateTime;} FILETIME;typedef union _ULARGE_INTEGER { struct { DWORD LowPart; DWORD HighPart; }; struct { DWORD LowPart; DWORD HighPart; } u; ULONGLONG QuadPart;} ULARGE_INTEGER;time_t FileTimeToTime_t(FILETIME* pFileTime){ time_t ret; ULARGE_INTEGER uli; uli.LowPart = pFileTime->dwLowDateTime; uli.HighPart = pFileTime->dwHighDateTime; ULONGLONG offset = 116444736; ret = (uli.QuadPart - (offset * 1000000000)) / 10000000; return ret;}// recivestatic int Receive(int fd, char* szText, int len){ int cnt; int rc; cnt = len; while (cnt>0){ rc = recv(fd, szText, cnt, 0); if (rc < 0) return -1; if (rc ==0 ) return len - cnt; szText += rc; cnt -= rc; } return len;}// sendstatic int Send(int fd, char* szText, int len){ int cnt; int rc; cnt = len; while (cnt > 0){ rc = send(fd, szText, cnt, 0); if (rc < 0) return -1; if (rc == 0) return len - cnt; szText += rc; cnt -= rc; } return len;}int update_time_from_server(const char* serverIP, int serverPort){ int fd; std::cout << "Create socket..."; fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0){ std::cout << "[fail]!" << std::endl; return -1; } std::cout << "[sucess]!" << std::endl; sockaddr_in local; memset(&local, 0, sizeof(local)); // setup the server data local.sin_family = AF_INET; local.sin_port = htons(serverPort); local.sin_addr.s_addr = inet_addr(serverIP); // try to connect to server... std::cout << "Connect to server..."; if (connect(fd, (sockaddr*)&local, sizeof(local)) < 0){ close(fd); std::cout << "[fail]!" << std::endl; return -1; } std::cout << "[sucess]!" << std::endl; // send the data to server, std::string data; data.resize(48); data[0] = 0x1B; std::cout << "Send test data to server..."; if (48 != Send(fd, &data[0], data.size())){ std::cout << "[fail]!" << std::endl; close(fd); return -1; } std::cout << "[sucess]!" << std::endl; std::cout << "Recive data from server..."; if (48 != Receive(fd, &data[0], data.size())){ std::cout << "[fail]!" << std:: endl; close(fd); return -1; } std::cout << "[success]!" << std::endl; std::cout << "Checking the data..." << std::endl; if (data[0] == 0x1C){ std::cout << "Data from server is valide!" << std::endl; FILETIME t2, t3; memcpy(&t2, &data[32], sizeof(FILETIME)); memcpy(&t3, &data[40], sizeof(FILETIME));#if 0 time_t* prawtime = (time_t*)&data[32]; tm* ptm = gmtime(prawtime); if (ptm != NULL){ std::cout << "[year/month/day/hour]: [" << ptm->tm_year << "/" << ptm->tm_mon << "/" << ptm->tm_mday << "/" << ptm->tm_hour << "]"; }#else time_t rawtime = FileTimeToTime_t(&t2); tm* ptm = gmtime(&rawtime); if (ptm != NULL){ std::cout << "[year/month/day hour:minut:second]: [" << ptm->tm_year + 1900 << "/" << ptm->tm_mon + 1 << "/" << ptm->tm_mday << " " << ptm->tm_hour+8 << ":" << ptm->tm_min << ":" << ptm->tm_sec << "]" << std::endl; } if (stime(&rawtime)){ std::cout << "Setting time error!" << std::endl; }#endif } else{ std::cout << "Data from server is invalide!" << std::endl; close(fd); return -1; } std::cout << "Done sucess!!!" << std::endl; close(fd); return 0;}int main(int argc, char* argv[]){ const unsigned int checkTime = 600; const char* serverIP = "127.0.0.1"; int serverPort = 19872; int checkNum = 1; bool daemonMode = false; if ( argc == 2 && strcmp (argv[1], "-d") == 0) { daemonMode = true; }#ifndef WIN32 signal (SIGPIPE, SIG_IGN ); signal (SIGSEGV, SIG_IGN ); signal (SIGALRM, SIG_IGN ); if (daemonMode) { daemon (1, 0); }#endif while (true) { std::cout << "Connect to server " << serverIP << " : " << serverPort << std::endl; update_time_from_server(serverIP, serverPort); checkNum ++; std::cout << "Try again " << checkTime << " seconds later..." << std::endl; sleep(checkTime); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -