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. }