Hello,
Here is an example of Multithreaded Pipe Server and Client using the
excellent ctypes library (Windows).
Reference - MSDN:
and
Best Regards,
Srijit
Named Pipe Server Source Code:
from ctypes import *
PIPE_ACCESS_DUP LEX = 0x3
PIPE_TYPE_MESSA GE = 0x4
PIPE_READMODE_M ESSAGE = 0x2
PIPE_WAIT = 0
PIPE_UNLIMITED_ INSTANCES = 255
BUFSIZE = 4096
NMPWAIT_USE_DEF AULT_WAIT = 0
INVALID_HANDLE_ VALUE = -1
ERROR_PIPE_CONN ECTED = 535
MESSAGE = "Default answer from server\0"
szPipename = "\\\\.\\pipe\\m ynamedpipe"
def ReadWrite_Clien tPipe_Thread(hP ipe):
chBuf = create_string_b uffer(BUFSIZE)
cbRead = c_ulong(0)
while 1:
fSuccess = windll.kernel32 .ReadFile(hPipe , chBuf, BUFSIZE,
byref(cbRead), None)
if ((fSuccess ==1) or (cbRead.value != 0)):
print chBuf.value
cbWritten = c_ulong(0)
fSuccess = windll.kernel32 .WriteFile(hPip e,
c_char_p(MESSAG E),
len(MESSAGE),
byref(cbWritten ),
None
)
else:
break
if ( (not fSuccess) or (len(MESSAGE) != cbWritten.value )):
print "Could not reply to the client's request from the
pipe"
break
else:
print "Number of bytes written:", cbWritten.value
windll.kernel32 .FlushFileBuffe rs(hPipe)
windll.kernel32 .DisconnectName dPipe(hPipe)
windll.kernel32 .CloseHandle(hP ipe)
return 0
def main():
THREADFUNC = CFUNCTYPE(c_int , c_int)
thread_func = THREADFUNC(Read Write_ClientPip e_Thread)
while 1:
hPipe = windll.kernel32 .CreateNamedPip eA(szPipename,
PIPE_ACCESS_DUP LEX,
PIPE_TYPE_MESSA GE |
PIPE_READMODE_M ESSAGE
|
PIPE_WAIT,
PIPE_UNLIMITED_ INSTANCES,
BUFSIZE, BUFSIZE,
NMPWAIT_USE_DEF AULT_WAIT,
None
)
if (hPipe == INVALID_HANDLE_ VALUE):
print "Error in creating Named Pipe"
return 0
fConnected = windll.kernel32 .ConnectNamedPi pe(hPipe, None)
if ((fConnected == 0) and (windll.kernel3 2.GetLastError( ) ==
ERROR_PIPE_CONN ECTED)):
fConnected = 1
if (fConnected == 1):
dwThreadId = c_ulong(0)
hThread = windll.kernel32 .CreateThread(N one, 0,
thread_func, hPipe, 0, byref(dwThreadI d))
if (hThread == -1):
print "Create Thread failed"
return 0
else:
windll.kernel32 .CloseHandle(hT hread)
else:
print "Could not connect to the Named Pipe"
windll.kernel32 .CloseHandle(hP ipe)
return 0
if __name__ == "__main__":
main()
Named Pipe Client Source Code:
from ctypes import *
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 0x3
INVALID_HANDLE_ VALUE = -1
PIPE_READMODE_M ESSAGE = 0x2
ERROR_PIPE_BUSY = 231
ERROR_MORE_DATA = 234
BUFSIZE = 512
MESSAGE = "Default message from client\0"
szPipename = "\\\\.\\pipe\\m ynamedpipe"
def main():
while 1:
hPipe = windll.kernel32 .CreateFileA(sz Pipename, GENERIC_READ |
GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, None)
if (hPipe != INVALID_HANDLE_ VALUE):
break
else:
print "Invalid Handle Value"
if (windll.kernel3 2.GetLastError( ) != ERROR_PIPE_BUSY ):
print "Could not open pipe"
return
elif ((windll.kernel 32.WaitNamedPip eA(szPipename, 20000)) ==
0):
print "Could not open pipe\n"
return
dwMode = c_ulong(PIPE_RE ADMODE_MESSAGE)
fSuccess = windll.kernel32 .SetNamedPipeHa ndleState(hPipe ,
byref(dwMode), None, None);
if (not fSuccess):
print "SetNamedPipeHa ndleState failed"
cbWritten = c_ulong(0)
fSuccess = windll.kernel32 .WriteFile(hPip e, c_char_p(MESSAG E),
len(MESSAGE), byref(cbWritten ), None)
if ((not fSuccess) or (len(MESSAGE) != cbWritten.value )):
print "Write File failed"
return
else:
print "Number of bytes written:", cbWritten.value
fSuccess = 0
chBuf = create_string_b uffer(BUFSIZE)
cbRead = c_ulong(0)
while (not fSuccess): # repeat loop if ERROR_MORE_DATA
fSuccess = windll.kernel32 .ReadFile(hPipe , chBuf, BUFSIZE,
byref(cbRead), None)
if (fSuccess == 1):
print "Number of bytes read:", cbRead.value
print chBuf.value
break
elif (windll.kernel3 2.GetLastError( ) != ERROR_MORE_DATA ):
break
windll.kernel32 .CloseHandle(hP ipe)
return
if __name__ == "__main__":
main()
Here is an example of Multithreaded Pipe Server and Client using the
excellent ctypes library (Windows).
Reference - MSDN:
and
Best Regards,
Srijit
Named Pipe Server Source Code:
from ctypes import *
PIPE_ACCESS_DUP LEX = 0x3
PIPE_TYPE_MESSA GE = 0x4
PIPE_READMODE_M ESSAGE = 0x2
PIPE_WAIT = 0
PIPE_UNLIMITED_ INSTANCES = 255
BUFSIZE = 4096
NMPWAIT_USE_DEF AULT_WAIT = 0
INVALID_HANDLE_ VALUE = -1
ERROR_PIPE_CONN ECTED = 535
MESSAGE = "Default answer from server\0"
szPipename = "\\\\.\\pipe\\m ynamedpipe"
def ReadWrite_Clien tPipe_Thread(hP ipe):
chBuf = create_string_b uffer(BUFSIZE)
cbRead = c_ulong(0)
while 1:
fSuccess = windll.kernel32 .ReadFile(hPipe , chBuf, BUFSIZE,
byref(cbRead), None)
if ((fSuccess ==1) or (cbRead.value != 0)):
print chBuf.value
cbWritten = c_ulong(0)
fSuccess = windll.kernel32 .WriteFile(hPip e,
c_char_p(MESSAG E),
len(MESSAGE),
byref(cbWritten ),
None
)
else:
break
if ( (not fSuccess) or (len(MESSAGE) != cbWritten.value )):
print "Could not reply to the client's request from the
pipe"
break
else:
print "Number of bytes written:", cbWritten.value
windll.kernel32 .FlushFileBuffe rs(hPipe)
windll.kernel32 .DisconnectName dPipe(hPipe)
windll.kernel32 .CloseHandle(hP ipe)
return 0
def main():
THREADFUNC = CFUNCTYPE(c_int , c_int)
thread_func = THREADFUNC(Read Write_ClientPip e_Thread)
while 1:
hPipe = windll.kernel32 .CreateNamedPip eA(szPipename,
PIPE_ACCESS_DUP LEX,
PIPE_TYPE_MESSA GE |
PIPE_READMODE_M ESSAGE
|
PIPE_WAIT,
PIPE_UNLIMITED_ INSTANCES,
BUFSIZE, BUFSIZE,
NMPWAIT_USE_DEF AULT_WAIT,
None
)
if (hPipe == INVALID_HANDLE_ VALUE):
print "Error in creating Named Pipe"
return 0
fConnected = windll.kernel32 .ConnectNamedPi pe(hPipe, None)
if ((fConnected == 0) and (windll.kernel3 2.GetLastError( ) ==
ERROR_PIPE_CONN ECTED)):
fConnected = 1
if (fConnected == 1):
dwThreadId = c_ulong(0)
hThread = windll.kernel32 .CreateThread(N one, 0,
thread_func, hPipe, 0, byref(dwThreadI d))
if (hThread == -1):
print "Create Thread failed"
return 0
else:
windll.kernel32 .CloseHandle(hT hread)
else:
print "Could not connect to the Named Pipe"
windll.kernel32 .CloseHandle(hP ipe)
return 0
if __name__ == "__main__":
main()
Named Pipe Client Source Code:
from ctypes import *
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 0x3
INVALID_HANDLE_ VALUE = -1
PIPE_READMODE_M ESSAGE = 0x2
ERROR_PIPE_BUSY = 231
ERROR_MORE_DATA = 234
BUFSIZE = 512
MESSAGE = "Default message from client\0"
szPipename = "\\\\.\\pipe\\m ynamedpipe"
def main():
while 1:
hPipe = windll.kernel32 .CreateFileA(sz Pipename, GENERIC_READ |
GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, None)
if (hPipe != INVALID_HANDLE_ VALUE):
break
else:
print "Invalid Handle Value"
if (windll.kernel3 2.GetLastError( ) != ERROR_PIPE_BUSY ):
print "Could not open pipe"
return
elif ((windll.kernel 32.WaitNamedPip eA(szPipename, 20000)) ==
0):
print "Could not open pipe\n"
return
dwMode = c_ulong(PIPE_RE ADMODE_MESSAGE)
fSuccess = windll.kernel32 .SetNamedPipeHa ndleState(hPipe ,
byref(dwMode), None, None);
if (not fSuccess):
print "SetNamedPipeHa ndleState failed"
cbWritten = c_ulong(0)
fSuccess = windll.kernel32 .WriteFile(hPip e, c_char_p(MESSAG E),
len(MESSAGE), byref(cbWritten ), None)
if ((not fSuccess) or (len(MESSAGE) != cbWritten.value )):
print "Write File failed"
return
else:
print "Number of bytes written:", cbWritten.value
fSuccess = 0
chBuf = create_string_b uffer(BUFSIZE)
cbRead = c_ulong(0)
while (not fSuccess): # repeat loop if ERROR_MORE_DATA
fSuccess = windll.kernel32 .ReadFile(hPipe , chBuf, BUFSIZE,
byref(cbRead), None)
if (fSuccess == 1):
print "Number of bytes read:", cbRead.value
print chBuf.value
break
elif (windll.kernel3 2.GetLastError( ) != ERROR_MORE_DATA ):
break
windll.kernel32 .CloseHandle(hP ipe)
return
if __name__ == "__main__":
main()
Comment