代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
网络时间同步本地电脑
作者:
旭子
/ 发布于
2013/10/24
/
764
/* Author <Dengxu> * Bug Report To <e-mail:hzdengxu@163.com> * Blogs <http://blog.csdn.net/dengxu11> * * ABOUT THE C PROGRAMMING LANGUAGE * DENNIS MACALISTAIR RITCHIE (BORN SEPTEM * -BER 9, 1941) IS THE CREATOR OF THE C PR * -OGRAMMING LANGUAGE AND A KEY DEVELOPER * OF THE UNIX OPERATING SYSTEM. ******************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <time.h> #include <dos.h> #ifdef WIN32 #include <winsock2.h> #include <windows.h> #ifndef _DEBUG //#pragma comment(linker, "/subsystem:/" windows/" /entry:/"mainCRTStartup/"") #endif #pragma comment(lib, "WS2_32.lib") #define close closesocket #define socklen_t int #else #include <netdb.h> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> #endif static void settime(struct tm *pt) { #ifdef WIN32 SYSTEMTIME ss; ss.wDay = pt->tm_mday; ss.wDayOfWeek = pt->tm_wday; ss.wHour = pt->tm_hour; ss.wMilliseconds = 0; ss.wMinute = pt->tm_min; ss.wMonth = pt->tm_mon + 1; ss.wSecond = pt->tm_sec; ss.wYear = pt->tm_year + 1900; if (FALSE == SetLocalTime(&ss)) { #ifdef _DEBUG printf("%d\n", GetLastError()); #endif // _DEBUG } #endif } // class CRegEditor // { // public: // const char *m_cstr_RegName; // HKEY hkResult; // // CRegEditor(void) : m_regname("Software\\Microsoft\\Windows\\CurrentVersion\\Run") // { // } // // void SetRegKeyStart(const char *regname) // { // // } // // // int _main(void) { char regname[] = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; HKEY hkResult; char modlepath[256]; char syspath[256]; GetModuleFileName(0, modlepath, 256); //取得程序名字 GetSystemDirectory(syspath, 256); int ret = CopyFile(modlepath, strcat(syspath, "\\Timed.exe"), 0); if(ret) { #ifdef _DEBUG printf("%s has been copyed to sys dir %s\n", modlepath, syspath); #endif // _DEBUG } else { #ifdef _DEBUG printf("%s is exisis", modlepath); #endif // _DEBUG } ret = RegOpenKey(HKEY_LOCAL_MACHINE, regname, &hkResult); ret = RegSetValueEx(hkResult, "Timed"/* 注册表键名 */, 0, REG_EXPAND_SZ, (unsigned char *)syspath, strlen(syspath) + 1); if (ret == 0) { #ifdef _DEBUG printf("success to write run key\n"); #endif // _DEBUG RegCloseKey(hkResult); } else { #ifdef _DEBUG printf("failed to open regedit.%dn",ret); #endif // _DEBUG return 0; } return 0; } int main__(int argc, char *argv[]); // int main(int argc, char *argv[]) // { // fclose(fopen("C:\\hahadengxu.txt", "w+")); // // while (0 != main__(argc, argv)) // { // Sleep(1000); // } // // return 0; // } int main__(int argc, char *argv[]) { if (argc == 2) { if (0 == stricmp("setup", argv[1])) { return _main(); } else { return 0; } } #ifdef WIN32 WSADATA ws; if (WSAStartup(MAKEWORD(2, 2), &ws) != 0) { printf("error!\n"); WSACleanup(); return 1; } #endif #ifdef WIN32 SOCKET sfd = socket(AF_INET, SOCK_DGRAM, 0); // IPPROTO_UDP struct sockaddr_in sin_; sin_.sin_family = AF_INET; sin_.sin_port = htons(123); sin_.sin_addr.s_addr = inet_addr("210.72.145.44"); #else SOCKET sfd = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in sin_; sin_.sin_family = AF_INET; sin_.sin_port = htons(123); sin_.sin_addr.s_addr = inet_addr("210.72.145.44"); #endif char buf[48] = {0}; socklen_t n = sizeof (sin_); int nMilliseconds = 3000; int nReturn = setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (CHAR *)&nMilliseconds, sizeof(nMilliseconds)); if (-1 == sendto(sfd, buf, 48, 0, (struct sockaddr *)&sin_, sizeof(struct sockaddr))) { #ifdef _DEBUG fprintf(stderr, "send failed!\n"); #endif // _DEBUG return 1; } if (SOCKET_ERROR == recvfrom(sfd, buf, 48, 0, (struct sockaddr *)&sin_, &n)) { #ifdef _DEBUG fprintf(stderr, "recvfrom failed!\n"); #endif // _DEBUG //goto again; return 1; } double r = 0.0; int i; unsigned char *p = (unsigned char *)buf + 40; for (i = 0; i < 8; i++) { r += p[i] * pow(2.0, (3 - i) * 8.0); } r -= 2208988800.0; time_t t = (time_t)r; struct tm *pt = localtime(&t); settime(pt); close(sfd); #ifdef WIN32 WSACleanup(); #endif return 0; } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int argc = 1; char *argv[3] = {"Timed.exe", NULL, NULL}; if (0 == stricmp("setup", lpCmdLine)) { argv[1] = "setup"; argc++; } while (0 != main__(argc, argv)) { Sleep(1000); } return 0; }
试试其它关键字
时间同步
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
旭子
贡献的其它代码
(
2
)
.
网络时间同步本地电脑
.
Windows下递归建立目录
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3