pipe server client通信

工程环境:vs2017

字符集为unicode, 所有用wchar_t;如果为多字符集,则用char. Server与Client的管道名称需一致。

工程中加入InitializeCriticalSection(cs);  的话,出现问题,所以暂时没有使用。

1.  // pipeServer.cpp: 定义控制台应用程序的入口点。  

2.  //  

3.    

4.  #include "stdafx.h"  

5.  #include <stdio.h>  

6.  #include <tchar.h>  

7.  #include <windows.h>  

8.  #include <process.h>  

9.  #include <stdlib.h>  

10. #include <conio.h>    

11.   

12. const wchar_t strPipeSend[30] = L"\\\\.\\pipe\\Pipe_demo_send";  

13. const wchar_t strPipeGet[30] = L"\\\\.\\pipe\\Pipe_demo_get";  

14.   

15.   

16. HANDLE get, mSend, mutex;  

17. //LPCRITICAL_SECTION cs;  

18.   

19. const int BUFFER_MAX_LEN = 1024;  

20. char bufW[BUFFER_MAX_LEN] = "hello client!";  

21. char bufR[BUFFER_MAX_LEN] = "";  

22. DWORD dwLen;  

23.   

24.   

25. HANDLE hThreadGet;  

26. HANDLE hThreadSend;  

27.   

28. WCHAR* toWChar(const char *c)  

29. {  

30.     WCHAR wszClassName[256];  

31.     memset(wszClassName, 0, sizeof(wszClassName));  

32.     MultiByteToWideChar(CP_ACP, 0, c, strlen(c) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));  

33.   

34.     return wszClassName;  

35. }  

36.   

37. DWORD WINAPI beginSendThread(PVOID p)  

38. {  

39.     printf("服务器send\n");  

40.     printf("等待连接...\n");  

41.   

42.     HANDLE hPipe = CreateNamedPipe(strPipeSend, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,  

43.         PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0);  

44.   

45.     if (ConnectNamedPipe(hPipe, NULL) != NULL)  

46.     {  

47.         printf("连接成功,开始发送缓冲区数据\n");  

48.         while (true)  

49.         {  

50.             //WaitForSingleObject(mutex, INFINITE);  

51.             //EnterCriticalSection(cs);  

52.             if(WriteFile(hPipe, bufW, sizeof(bufW), &dwLen, NULL))  

53.             {  

54.                 printf("数据写入完毕共%d字节\n", dwLen);  

55.             }  

56.             else  

57.             {  

58.                 printf("数据写入失败\n");  

59.             }  

60.             //LeaveCriticalSection(cs);  

61.             Sleep(500);  

62.             //ReleaseSemaphore(mutex, 1, NULL);  

63.         }  

64.         //_endthread();  

65.     }  

66.   

67.     return 0;  

68. }  

69.   

70. DWORD WINAPI beginGetThread(PVOID p)  

71. {  

72.     printf("服务器Get\n");  

73.     printf("等待连接....\n");  

74.     HANDLE hPipe = CreateNamedPipe(strPipeGet, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,  

75.         PIPE_UNLIMITED_INSTANCES, 0, 0, NMPWAIT_WAIT_FOREVER, 0);  

76.   

77.     if (ConnectNamedPipe(hPipe, NULL) != NULL)  

78.     {  

79.         printf("连接成功,开始接收数据\n");  

80.         while (true)  

81.         {  

82.             //WaitForSingleObject(mutex, INFINITE);  

83.             //EnterCriticalSection(cs);  

84.             //接收客户端发送的数据  

85.             ReadFile(hPipe, bufR, BUFFER_MAX_LEN, &dwLen, NULL);  

86.             printf("接收到来自A的数据长度为%d字节\n", dwLen);  

87.             printf("具体数据内容如下:");  

88.             int bufSize;  

89.             for (bufSize = 0; bufSize < (int)dwLen; bufSize++)  

90.             {  

91.                 putchar(bufR[bufSize]);  

92.             }  

93.             //LeaveCriticalSection(cs);  

94.             Sleep(500);  

95.             //ReleaseSemaphore(mutex, 1, NULL);  

96.             putchar('\n');  

97.   

98.         }  

99.         //_endthread();  

100.     }  

101.   

102.     return 0;  

103. }  

104.   

105. int main()  

106. {  

107.     printf("服务器开始...");  

108.     //cs = (LPCRITICAL_SECTION)malloc(sizeof(LPCRITICAL_SECTION));  

109.     //InitializeCriticalSection(cs);  

110.     mutex = CreateSemaphore(NULL, 1, 1, TEXT("mutex"));  

111.   

112.     //_beginthread(beginGetThread, NULL, NULL);  

113.     //_beginthread(beginSendThread, NULL, NULL);  

114.     //CreateThread(NULL, 0, beginSendThread, NULL, 0, NULL);  

115.     CreateThread(NULL, 0, beginGetThread, NULL, 0, NULL);  

116.   

117.     Sleep(INFINITE);  

118.     //DeleteCriticalSection(cs);  

119.   

120.   

121.     return 0;  

122. }  

1.  // PipeClient.cpp: 定义控制台应用程序的入口点。  

2.  //  

3.    

4.  #include "stdafx.h"  

5.  #include <stdio.h>  

6.  #include <tchar.h>  

7.  #include <windows.h>  

8.  #include <process.h>  

9.  #include <conio.h>    

10. #include <stdlib.h>  

11.   

12. const wchar_t StrPipeNameSend[30] = L"\\\\.\\pipe\\Pipe_demo_send";  

13. const wchar_t StrPipeNameGet[30] = L"\\\\.\\pipe\\Pipe_demo_get";  

14.   

15. const int BUFFER_MAX_LEN = 1024;  

16. char bufW[BUFFER_MAX_LEN] = "hello Server!";  

17. char bufR[BUFFER_MAX_LEN] = "";  

18.   

19. HANDLE get, mSend, mutex;  

20. //LPCRITICAL_SECTION cs;  

21.   

22.   

23. WCHAR* toWChar(const char *c)  

24. {  

25.     WCHAR wszClassName[256];  

26.     memset(wszClassName, 0, sizeof(wszClassName));  

27.     MultiByteToWideChar(CP_ACP, 0, c, strlen(c) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));  

28.   

29.     return wszClassName;  

30. }  

31.   

32.   

33. DWORD WINAPI beginSendThread(PVOID p)  

34. {  

35.     if (WaitNamedPipe(StrPipeNameGet, NMPWAIT_WAIT_FOREVER))  

36.     {  

37.         printf("Success!连接Server Get成功;\n");  

38.         HANDLE hPipe = CreateFile(StrPipeNameGet, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  

39.   

40.         while (true)  

41.         {  

42.             //WaitForSingleObject(mutex, INFINITE);  

43.             //EnterCriticalSection(cs);  

44.             printf("请输入要向服务器发送的数据,回车键结束,最大1024个字节\n");  

45.             DWORD dwLen;  

46.   

47.             if (WriteFile(hPipe, bufW, sizeof(bufW), &dwLen, NULL))  

48.             {  

49.                 printf("数据写入完毕工%d字节\n", dwLen);  

50.             }  

51.             else  

52.             {  

53.                 printf("数据写入失败\n");  

54.             }  

55.             //LeaveCriticalSection(cs);  

56.             Sleep(500);  

57.             //ReleaseSemaphore(mutex, 1, NULL);  

58.   

59.         }  

60.   

61.         CloseHandle(hPipe);  

62.         //_endthread();  

63.     }  

64.   

65.     DWORD dw = GetLastError();  

66.   

67.     return 0;  

68.   

69. }  

70.   

71. DWORD WINAPI beginGetThread(PVOID p)  

72. {  

73.     if (WaitNamedPipe(StrPipeNameSend, NMPWAIT_WAIT_FOREVER))  

74.     {  

75.         printf("Success!连接ServerSend成功;\n");  

76.   

77.         HANDLE hPipeSend = CreateFile(StrPipeNameSend, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  

78.   

79.         while (true)  

80.         {  

81.             //WaitForSingleObject(mutex, INFINITE);  

82.             //EnterCriticalSection(cs);  

83.             DWORD dwLen;  

84.   

85.             if (ReadFile(hPipeSend, bufR, sizeof(bufR), &dwLen, NULL))  

86.             {  

87.                 printf("数据读取完毕共%d字节\n", dwLen);  

88.             }  

89.             else  

90.             {  

91.                 printf("数据读取失败\n");  

92.             }  

93.             //LeaveCriticalSection(cs);  

94.             Sleep(500);  

95.             //ReleaseSemaphore(mutex, 1, NULL);  

96.   

97.         }  

98.   

99.         CloseHandle(hPipeSend);  

100.         //_endthread();  

101.     }  

102.   

103.     DWORD dw = GetLastError();  

104.   

105.     return 0;  

106. }  

107.   

108. int main()  

109. {  

110.     printf("Client开始等待...\n");  

111.   

112.     //cs = (LPCRITICAL_SECTION)malloc(sizeof(LPCRITICAL_SECTION));  

113.     //InitializeCriticalSection(cs);  

114.     mutex = CreateSemaphore(NULL, 1, 1, TEXT("mutex"));  

115.   

116.     //_beginthread(beginGetThread,NULL, NULL);  

117.     //_beginthread(beginSendThread, NULL, NULL);  

118.     CreateThread(NULL, 0, beginGetThread, NULL, 0, NULL);  

119.     CreateThread(NULL, 0, beginSendThread, NULL, 0, NULL);  

120.       

121.     Sleep(INFINITE);  

122.     //DeleteCriticalSection(cs);      

123.   

124.     return 0;  

125. }  


猜你喜欢

转载自blog.csdn.net/bubuxindong/article/details/80843585