資源簡介
實現了c++與c#之間的命名管道通信,其中c++作為服務器端,c#作為客戶端

代碼片段和文件信息
//server
//命名管道采用基于連接的可靠傳輸方式只能一對一傳輸
#include?
#include?
#include?
#define?BUF_SIZE?1024
using?namespace?std;
int?main()
{
string?s;
HANDLE?h_pipe;
char?buf_msg[BUF_SIZE];
DWORD?num_rcv;?//實際接收到的字節數
//創建命名管道命名為mynamedpipe消息只能從客戶端流向服務器讀寫數據采用阻塞模式字節流形式超時值置為0表示采用默認的50毫秒
h_pipe?=?CreateNamedPipe(TEXT(“\\\\.\\pipe\\mynamedpipe“)?PIPE_ACCESS_DUPLEX
PIPE_TYPE_MESSAGE?|PIPE_READMODE_MESSAGE?|?PIPE_WAIT?PIPE_UNLIMITED_INSTANCES
BUF_SIZE?BUF_SIZE?0?NULL);
if?(h_pipe?==?INVALID_HANDLE_VALUE)
{
cerr?<“Failed?to?create?named?pipe!Error?code:?“?< return?-1;
}
else
{
cout?<“Named?pipe?created?successfully...\n“;
}
//等待命名管道客戶端連接,一直在等待
if?(ConnectNamedPipe(h_pipe?nullptr))
{
cout?<“A?client?connected...\n“;
memset(buf_msg?0?BUF_SIZE);
char?writebuf[256]?=?“you?are?the?best!\n“;
DWORD?writelen?=?0;
int?a;
while?(1)
{
cout?<“輸入任一值然后按Enter進行發送:“;
cin?>>?a;
if?(WriteFile(h_pipewritebuf?sizeof(writebuf)?&writelen?NULL))
{
cout?<“成功發送數據“?< }
else
{
cerr?<“Failed?to?send?message!Error?code:?“?< CloseHandle(h_pipe);
system(“pause“);
return?1;
}
}
}
CloseHandle(h_pipe);
system(“pause“);
return?0;
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2019-04-13?21:50??Cpp和CSharp管道\
?????文件????????2779??2019-03-25?09:31??Cpp和CSharp管道\cpp_csharp_pipe.sln
?????目錄???????????0??2019-04-13?21:51??Cpp和CSharp管道\cpp端\
?????文件??????327680??2019-03-22?14:53??Cpp和CSharp管道\cpp端\cpp端.sdf
?????文件????????9728??2019-03-22?14:53??Cpp和CSharp管道\cpp端\cpp端.v12.suo
?????文件????????5936??2019-03-22?14:51??Cpp和CSharp管道\cpp端\cpp端.vcxproj
?????文件?????????949??2019-03-22?14:51??Cpp和CSharp管道\cpp端\cpp端.vcxproj.filters
?????文件?????????165??2019-03-22?14:50??Cpp和CSharp管道\cpp端\cpp端.vcxproj.user
?????目錄???????????0??2019-03-25?09:39??Cpp和CSharp管道\cpp端\Debug\
?????文件????????1486??2019-04-13?21:45??Cpp和CSharp管道\cpp端\main.cpp
?????目錄???????????0??2019-03-25?09:45??Cpp和CSharp管道\cpp端\x64\
?????目錄???????????0??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\
?????文件?????????204??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.log
?????目錄???????????0??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\
?????文件????????1178??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\CL.command.1.tlog
?????文件???????40868??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\CL.read.1.tlog
?????文件?????????356??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\CL.write.1.tlog
?????文件?????????208??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\cpp端.lastbuildstate
?????文件????????2198??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\li
?????文件????????3672??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\li
?????文件?????????342??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\cpp端.tlog\li
?????文件??????158957??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\main.obj
?????文件??????601088??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\vc141.idb
?????文件??????503808??2019-04-13?21:45??Cpp和CSharp管道\cpp端\x64\Debug\vc141.pdb
?????目錄???????????0??2019-03-25?10:48??Cpp和CSharp管道\csharp端\
?????文件?????????189??2019-03-22?14:52??Cpp和CSharp管道\csharp端\App.config
?????目錄???????????0??2019-03-22?14:52??Cpp和CSharp管道\csharp端\bin\
?????目錄???????????0??2019-03-22?14:54??Cpp和CSharp管道\csharp端\bin\Debug\
?????文件????????5632??2019-04-13?21:34??Cpp和CSharp管道\csharp端\bin\Debug\csharp端.exe
?????文件?????????189??2019-03-22?14:52??Cpp和CSharp管道\csharp端\bin\Debug\csharp端.exe.config
?????文件???????15872??2019-04-13?21:34??Cpp和CSharp管道\csharp端\bin\Debug\csharp端.pdb
............此處省略23個文件信息
- 上一篇:上海電機學院C語言實訓答案
- 下一篇:MFC 打開文件夾
評論
共有 條評論