这个问题该如何解决?

时间已经不多了 2006-09-18 07:51:17
做了个监视程序,能够自动启动一些EXE的程序,并且隔段时间就对被启动的程序进行一次通信,若客户端程序死了,就关掉并重起,服务程序大致如下:
#include <windows.h>
#include <Tlhelp32.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define maxline 200
#define ID_TIMER 1

void rpath();
void scanEXE(char *,char *); //扫描进程
void creatpipe();//创建命名管道
void createpro(char *);

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,

PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("Test") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra= 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon= LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor= LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;

if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("Program requires Windows!"),szAppName, MB_ICONERROR);
return 0 ;
}

hwnd = CreateWindow (szAppName, TEXT ("调度程序"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;

ShowWindow (hwnd, SW_SHOWNORMAL) ; //窗体隐藏
UpdateWindow (hwnd) ;

while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}

return msg.wParam ;

}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam)
{
static BOOL fFlipFlop = FALSE ;

HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;

switch (message)
{
case WM_CREATE:
SetTimer (hwnd, ID_TIMER, 5000, NULL) ;
return 0 ;
case WM_TIMER :
// MessageBeep (-1) ;
// fFlipFlop = !fFlipFlop ;
rpath();
// scanEXE(); //扫描进程
// InvalidateRect (hwnd, NULL, FALSE) ;
return 0 ;
case WM_PAINT :
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("调度程序"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY :
KillTimer (hwnd, ID_TIMER) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;

}

void rpath()
{ FILE *fp;
int i,j;
char buf[maxline],fname[100],fpath[maxline];
if((fp=fopen("E:\\电子图书\\c\\启动\\scanExe\\123.txt","r+"))==NULL)
{ MessageBox (NULL, TEXT ("Can not open file 123.txt!"), TEXT ("OPEN FILE"), 0); exit(0); }

// fscanf(fp,"%d",&maxfile);

while (!feof(fp))
{ fgets(buf,maxline,fp);
if(buf[strlen(buf)-1]=='\n')
buf[strlen(buf)-1]='\0'; //去掉回车符号
for(i=0;buf[i]!='=';fname[i]=buf[i],i++); fname[i]='\0'; //获取可执行文件名
//获取可执行文件路径
for(i=i+1,j=0;i<strlen(buf);i++,j++)
fpath[j]=buf[i]; fpath[j]='\0';
strcat(fpath,fname);
scanEXE(fname,fpath);
}
fclose(fp);
}

void scanEXE(char *fname,char *fpath)
{ char TargetName[]="client.exe",AppName[]="c:\\client.exe"; //要寻找进程的文件名(测试用)
char toDisptxt[20];
int flag;
HANDLE Snapshot; //内存进程的“快照”句柄
PROCESSENTRY32 processListStr;
BOOL return_value;
static HANDLE ssnpPipe; //命名管道
DWORD NumBytesRead;
flag=1;
//返回内存所有进程的快照,参数为TH32CS_SNAPPROCESS取有的进程


Snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);

processListStr.dwSize=sizeof(PROCESSENTRY32);

//取快照中的第一个进程条目,成功返回一个TRUE,反之FALSE
return_value=Process32First(Snapshot,&processListStr);
//把快照中所有找到的进程的ID保存在向量ProcessID中
while(return_value)
{
if( (strcmp(fname,processListStr.szExeFile))==0 ) //程序在进程中
{
//MessageBox (NULL, TEXT ("Process was exist!"), TEXT ("CreateProcess"), 0);
flag=0;
}
return_value=Process32Next(Snapshot,&processListStr);
}
if(flag)
{
//MessageBox (NULL, TEXT ("program was not exist!"), TEXT ("CreateProcess"), 0);
ssnpPipe=CreateNamedPipe("\\\\.\\pipe\\ssnp", //创建命名管道
PIPE_ACCESS_INBOUND,
PIPE_TYPE_MESSAGE | PIPE_WAIT,
1, 0, 0, 150,
(LPSECURITY_ATTRIBUTES) NULL);
if (ssnpPipe == INVALID_HANDLE_VALUE)
{
MessageBox (NULL, TEXT ("Can not create name Pipe!"), TEXT ("CreateProcess"), 0);
}
createpro(fpath); //启动程序
if(!ConnectNamedPipe(ssnpPipe, //链接失败
(LPOVERLAPPED) NULL))
{
MessageBox (NULL, TEXT ("Connecting fail!"), TEXT ("CreateProcess"), 0);
CloseHandle(ssnpPipe);
}
}
if (!ReadFile(ssnpPipe, toDisptxt, //进行通信
sizeof(toDisptxt),
&NumBytesRead, (LPOVERLAPPED) NULL))
{
MessageBox (NULL, TEXT ("Unable read client information!"), TEXT ("CreateProcess"), 0);
CloseHandle(ssnpPipe); CloseHandle(Snapshot);
}
}


void createpro(char *fpath)
{ PROCESS_INFORMATION myProcInfo;
STARTUPINFO myStartInfo;
// char appName1[] = "C:\\client.exe"; //测试用的文件路径
int flag;

flag=0;
GetStartupInfo(&myStartInfo);
myStartInfo.cb = sizeof(STARTUPINFO);
myStartInfo.lpReserved = NULL;
myStartInfo.lpReserved2 = NULL;
myStartInfo.cbReserved2 = 0;
myStartInfo.lpDesktop = NULL;
myStartInfo.dwFlags = 0;
flag = CreateProcess(fpath, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &myStartInfo, &myProcInfo);

}
从123.txt文件中读取要启动的程序,先扫描可执行程序是否在进程中,若不在则创建。

客户端程序如下:
#include "stdafx.h"
#include <iostream>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include "stdio.h"
#include "dos.h"
#include "Windows.h"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
char *toSendtxt="Test String";
HANDLE ssnpPipe;
DWORD NumBytesWritten;
//char machineName[80];
char *machineName;
char pipeName[80];

//cout << "Enter name of server machine: ";
//cin >> machineName;
cout << "start!";
machineName = "127.0.0.1";
wsprintf(pipeName, "\\\\%s\\pipe\\ssnp",
machineName);

/* Create the named pipe file handle for sending
messages */
ssnpPipe=CreateFile(pipeName,
GENERIC_WRITE, FILE_SHARE_READ,
(LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

/* Check and see if the named pipe file was
opened, if not terminate program */
if (ssnpPipe == INVALID_HANDLE_VALUE)
{
cerr << "ERROR: Unable to create a named pipe "
<< endl;
cerr << GetLastError() << endl;
return (1);
}

/* Repeatedly send message until program is
terminated */
while(1)
{
cout << "Sending..." << endl;
/* Write message to the pipe */
if (!WriteFile(ssnpPipe,
toSendtxt, (DWORD) strlen(toSendtxt)+1,
&NumBytesWritten, (LPOVERLAPPED) NULL))
{
/* If error occurs when writing to named
pipe, terminate program */
cerr << "ERROR: Unable to write to named pipe "
<< GetLastError() << endl;
CloseHandle(ssnpPipe);
return (1);
}

/* Wait before sending the message again */
Sleep(4800);
} /* while*/

return 0;
}
客户端是别人编写的,在控制台下可以运行。
现在的问题是服务端第一次可以启动客户端的程序,并且也可以通信,但,关掉客户端后,就再启动不了了,这要怎么解决啊?如果还有其他的一些问题,也希望能够指出,谢谢了。
...全文
114 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2006-09-18
  • 打赏
  • 举报
回复
客户端退出后,
服务端是不是也 over 了 ?

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧