服务端
[code=C/C++][#include <windows.h>
#include <stdio.h>
#include <winsock.h>
#pragma comment(lib, "Wsock32.lib ")
#include <string.h>
#define PORT 27015
#define SLEEP_TIME 5000 //两次连续查询之间的的毫秒间隔
#define LOGFILE "D:\\memstatus1.txt"
////////////////////////////////////////////////////////////
// Declare several global variables to share
// their values across multiple functions of your program.
////////////////////////////////////////////////////////////
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
STARTUPINFO si,si1;
PROCESS_INFORMATION pi,pi1;//进程信息
TCHAR *Sexe; //存储可执行文件路径
TCHAR *Sexe1;
SOCKET sockfd;
struct sockaddr_in seraddr;
struct sockaddr_in cliaddr;
char buffer[5]="ok";
void WriteToLog(char* str)
{
FILE* log;
log = fopen(LOGFILE, "a+");
if (log == NULL){
OutputDebugString("Log file open failed.");
return ;
}
//strcat(str," Creat Error");
fprintf(log, "%s\n", str);
fclose(log);
return ;
}
int process_run(TCHAR *Sexe,STARTUPINFO &ssi,PROCESS_INFORMATION &ppi)
{
if (!CreateProcess(Sexe,NULL,NULL,NULL,FALSE,0,NULL,NULL,&ssi,&ppi))
{
WriteToLog(Sexe);
return 1;
}
return 0;
}
void udp_server()
{
WSADATA ws;//存放windows socket初始化信息
int ret=WSAStartup(MAKEWORD(2,2),&ws);//初始化windows Socket 2.2
if (ret!= 0)
{
WriteToLog("Init Windows Socket Failed!\n");
return ;
}
WriteToLog("服务器开始创建SOCKET\n");
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(PORT);//监听端口
seraddr.sin_addr.s_addr = htonl(INADDR_ANY);//
//memset(&(seraddr.sin_zero),0,8);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if (sockfd == INVALID_SOCKET)
{
WriteToLog("Socket Create Failed!\n");
return ;
}
ret = bind(sockfd,(struct sockaddr*)&seraddr,sizeof(seraddr));
if (ret == -1)
{
WriteToLog("Socket Bind Failed!\n");
closesocket(sockfd);
return ;
}
}
// Service initialization
int InitService()
{
udp_server();//创建socket
int result1,result2;
ZeroMemory(&si,sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi,sizeof (pi));
ZeroMemory(&si1,sizeof(si1));
si.cb = sizeof(si1);
ZeroMemory(&pi1,sizeof (pi1));
Sexe=TEXT("C:\\Program Files\\kuwo\\KWMUSIC\\bin\\KwMusic.exe");
//Sexe=TEXT("C:\\Program Files\\Qt\\w1\\debug\\w1.exe");
Sexe1=TEXT("C:\\Program Files\\duowan\\yy-4\\YY.exe");
result1=process_run(Sexe,si,pi);
result2=process_run(Sexe1,si1,pi1);
return result1||result2;
//return result1;
}
// Control Handler
void ControlHandler(DWORD request)
{