服务器端
客户端,注意我的服务器端是叫myserver的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using
namespace
std;
int
main(
int
argc,
char
* argv[])
{
cout<<
"server start ..."
<<endl;
MSG msg;
while
(GetMessage(&msg,NULL,0,0))
{
if
(WM_USER+1000 == msg.message)
{
cout<<
"get client msg and parameter is "
<<msg.lParam<<endl;
}
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
cout<<
"server exit ..."
<<endl;
return
0;
}
|
客户端,注意我的服务器端是叫myserver的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include "stdafx.h"
#include <Windows.h>
#include <Tlhelp32.h>
#include <iostream>
using
namespace
std;
int
main(
int
argc,
char
* argv[])
{
HANDLE
hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 info;
info.dwSize =
sizeof
(PROCESSENTRY32);
BOOL
bGetmore = Process32First(hProcess,&info);
BOOL
bfind = FALSE;
DWORD
taget = 0;
while
(bGetmore && !bfind)
{
if
(0==
strcmp
(
"myserver.exe"
,info.szExeFile))
{
cout<<
"process id "
<<info.th32ProcessID<<endl;
HANDLE
hModule = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
THREADENTRY32 tinfo;
tinfo.dwSize =
sizeof
(THREADENTRY32);
bGetmore = Thread32First(hModule, &tinfo);
while
(bGetmore)
{
if
(info.th32ProcessID==tinfo.th32OwnerProcessID)
{
cout<<
"thread id is "
<<tinfo.th32ThreadID<<endl;
bfind = TRUE;
taget = tinfo.th32ThreadID;
break
;
}
bGetmore = Thread32Next(hModule,&tinfo);
}
CloseHandle(hModule);
break
;
}
bGetmore = Process32Next(hProcess, &info);
}
CloseHandle(hProcess);
if
(!bfind)
{
return
0;
}
int
i = 0;
while
(1)
{
if
(::PostThreadMessage(taget,WM_USER+1000,0,++i))
{
cout<<
"send message "
<<i<<endl;
}
else
{
cout<<
"fail at "
<<i<<
","
<<GetLastError()<<endl;
}
Sleep(1000);
}
return
0;
}
|