import win32pipe, win32file, pywintypes
def recv():
print("pipe recv")
quit = False
while not quit:
try:
handle = win32pipe.CreateNamedPipe(
# r'\\.\pipe\Foo',
r'\\.\\Pipe\\mypipe',
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
0,
None)
win32pipe.ConnectNamedPipe(handle, None)
while True:
with torch.no_grad():
#resp = win32file.ReadFile(handle, 320*240*4)[1]
win32file.WriteFile(handle, some_data)
except pywintypes.error as e:
if e.args[0] == 2:
print("no pipe, trying again in a sec")
sleep(1)
elif e.args[0] == 109:
print("broken pipe, bye bye")
quit = True
if __name__ == '__main__':
recv()
c++端
#include "stdafx.h"
int main()
{
DWORD wlen = 0;
BOOL bRet = WaitNamedPipe(TEXT("\\\\.\\Pipe\\kat_walk"), NMPWAIT_WAIT_FOREVER);
if (!bRet)
{
printf("connect the namedPipe failed!\n");
return 0;
}
//pipe通道——————————————————————————————————————————————————————————————————————————
HANDLE hPipe = CreateFile( //管道屬于一種特殊的檔案
TEXT("\\\\.\\Pipe\\kat_walk"), //建立的檔案名
GENERIC_READ | GENERIC_WRITE, //檔案模式
0, //是否共享
NULL, //指向一個SECURITY_ATTRIBUTES結構的指針
OPEN_EXISTING, //建立參數
FILE_ATTRIBUTE_NORMAL, //檔案屬性(隐藏,隻讀)NORMAL為預設屬性
NULL); //模闆建立檔案的句柄
if (INVALID_HANDLE_VALUE == hPipe)
{
printf("open the exit pipe failed!\n");
exit(0);
}
while (true) {
char * dataT = new char[1];
DWORD rlen = 0;
std::cout<<ReadFile(hPipe,dataT, 4, &rlen, NULL);
//std::cout << " 接受到的資料為" <<(int)*dataT<< ","<< std::endl;
}
return 0;
}