注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Mihooke's blog

IT之恋

 
 
 

日志

 
 

创建子进程来建立多个client连接  

2017-05-13 19:40:58|  分类: C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Master要和2个worker:worker1和Woeker2通信,当Master和worker1建立通信之后,可以创建一个子进程SubMaster,SunMaster和Worker2进行通信,从而达到server和多个client进行通信的目的。

Master.cpp

#include <iostream>
#include <WinSock2.h>
#include <wchar.h>
#include <tchar.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")

#define MYNAMEDPIPE _T("\\\\.\\pipe\\MIHOOKE")
int main()
{
WORD wVersionRequired;
WSADATA wsaData;
wVersionRequired = MAKEWORD(2, 2);
int nErr = WSAStartup(wVersionRequired, &wsaData);
if (nErr != 0)
{
cout<<"WSAStartup ret error"<<endl;
WSACleanup();
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 && HIBYTE(wsaData.wVersion) != 2)
{
cout<<"BYTE error"<<endl;
WSACleanup();
return 2;
}

SOCKET sockMaster = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sockMaster)
{
WSACleanup();
return 3;
}
SOCKADDR_IN addrMaster;
addrMaster.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrMaster.sin_family = AF_INET;
addrMaster.sin_port = htons(1234);

int nRet = 0;
nRet = bind(sockMaster, (SOCKADDR*)&addrMaster, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
closesocket(sockMaster);
WSACleanup();
return 4;
}

nRet = listen(sockMaster, 10);
if (SOCKET_ERROR == nRet)
{
closesocket(sockMaster);
WSACleanup();
return 5;
}

SOCKET sockWorker;
SOCKADDR_IN addrWorker;
int nLen = sizeof(SOCKADDR);
sockWorker = accept(sockMaster, (SOCKADDR*)&addrWorker, &nLen);
if (INVALID_SOCKET == sockWorker)
{
closesocket(sockMaster);
WSACleanup();
return 6;
}
wchar_t subname[] = _T("..\\Debug\\SubMaster.exe");

// 创建命名管道
HANDLE hdPipe;
hdPipe = CreateNamedPipe(MYNAMEDPIPE,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
0,
0,
NMPWAIT_WAIT_FOREVER,
0);
if (INVALID_HANDLE_VALUE == hdPipe)
{
closesocket(sockWorker);
closesocket(sockMaster);
WSACleanup();
return 7;
}
STARTUPINFO si={sizeof(si)};
PROCESS_INFORMATION pi;
GetStartupInfo(&si);
if (!CreateProcess(subname, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
cout<<"CreateProcess fail"<<endl;
CloseHandle(hdPipe);
closesocket(sockMaster);
WSACleanup();
return 8;
}
if (ConnectNamedPipe(hdPipe, NULL) == NULL)
{
cout<<"connect pipe fail"<<endl;
CloseHandle(hdPipe);
closesocket(sockMaster);
WSACleanup();
return 9;
}

while (1)
{
char cBufSend[20] = {0};
cin>>cBufSend;
if (cBufSend[0] != 's')
{
continue;
}
send(sockWorker, "can send to me result", 22, 0);
char cBufRecv[20] = {0};
recv(sockWorker, cBufRecv, 20, 0);
cout<<cBufRecv<<endl;
DWORD dwLen = 0;
WriteFile(hdPipe, cBufRecv, 20, &dwLen, NULL);
}

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
closesocket(sockMaster);
WSACleanup();
return 0;
}

SubMaster.cpp

/*
***创建子进程来处理来自worker2的通讯
*/
#include <iostream>
#include <WinSock2.h>
#include <tchar.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")
#define MYNAMEDPIPE _T("\\\\.\\pipe\\MIHOOKE")
int main()
{
WORD wVersionRequired;
WSADATA wsaData;
wVersionRequired = MAKEWORD(2, 2);
int nErr = WSAStartup(wVersionRequired, &wsaData);
if (nErr != 0)
{
cout<<"WSAStartup ret error"<<endl;
WSACleanup();
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 && HIBYTE(wsaData.wVersion) != 2)
{
cout<<"BYTE error"<<endl;
WSACleanup();
return 2;
}

SOCKET sockSubMaster = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sockMaster)
{
WSACleanup();
return 3;
}
SOCKADDR_IN addrSubMaster;
addrSubMaster.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSubMaster.sin_family = AF_INET;
addrSubMaster.sin_port = htons(9527);

int nRet = 0;
nRet = bind(sockSubMaster, (SOCKADDR*)&addrMaster, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
closesocket(sockSubMaster);
WSACleanup();
return 4;
}

nRet = listen(sockSubMaster, 10);
if (SOCKET_ERROR == nRet)
{
closesocket(sockSubMaster);
WSACleanup();
return 5;
}

SOCKET sockPLC;
SOCKADDR_IN addrPLC;
int nLen = sizeof(SOCKADDR);
sockPLC = accept(sockSubMaster, (SOCKADDR*)&addrPLC, &nLen);
if (INVLAID_SOCKET == sockPLC)
{
closesocket(sockSubMaster);
WSACleanup();
return 6;
}

HANDLE hdPipe;
if (WaitNamedPipe(MYNAMEDPIPE, NMPWAIT_WAIT_FOREVER) == FALSE)
{
cout<<"wait namedpipe fail"<<endl;
CloseHandle(hdPipe);
closesocket(sockPLC);
WSACleanup();
return 7;
}
hdPipe = CreateFile(MYNAMEDPIPE,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (INVALID_HANDLE_VALUE == hdPipe)
{
CloseHandle(hdPipe);
closesocket(sockPLC);
WSACleanup();
return 8;
}
while (1)
{
char cBufSend[30] = {0};
DWORD dwLen = 0;
// 从父进程的管道流中读取数据
if (ReadFile(hdPipe, cBufSend, 30, &dwLen, NULL))
{
cout<<cBufSend<<endl;
send(sockPLC, cBufSend, strlen(cBufSend), 0);
}
else
{
//cout<<"read pipe fail"<<endl;
continue;
}
}

CloseHandle(hdPipe);
closesocket(sockPLC);
closesocket(sockSubMaster);
WSACleanup();
return 0;
}

Worker1.cpp

#include <iostream>
#include <WinSock2.h>
using namespace std;
#pragma comment(lib, "ws2_32.lib")

int main()
{
WORD wVersionRequsted;
WSADATA wsaData;
wVersionRequsted = MAKEWORD(2, 2);
int nErr = WSAStartup(wVersionRequsted, &wsaData);
if (nErr != 0)
{
cout<<"WSAStartup ret error"<<endl;
WSACleanup();
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 && HIBYTE(wsaData.wVersion) != 2)
{
cout<<"BYTE error"<<endl;
WSACleanup();
return 2;
}

SOCKET sockWorker = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sockWorker)
{
WSACleanup();
return 3;
}
SOCKADDR_IN addrWorker;
addrWorker.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrWorker.sin_family = AF_INET;
addrWorker.sin_port = htons(1234);

int nRet = 0;
nRet = connect(sockWorker, (SOCKADDR*)&addrWorker, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
closesocket(sockWorker);
WSACleanup();
return 4;
}

while (1)
{
char cBufRecv[25] = {0};
recv(sockWorker, cBufRecv, 25, 0);
cout<<cBufRecv<<endl;
string strRecv = cBufRecv;
if (!strRecv.compare("can send to me result"))
{
char cBufSend[20] = "parameter";
send(sockWorker, cBufSend, strlen(cBufSend), 0);
}
}
closesocket(sockWorker);
WSACleanup();
return 0;
}

Worker2.cpp

#include <WinSock2.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "ws2_32.lib")

int main()
{
WORD wVersionRequsted;
WSADATA wsaData;
wVersionRequsted = MAKEWORD(2, 2);
int nErr = WSAStartup(wVersionRequsted, &wsaData);
if (nErr != 0)
{
WSACleanup();
return 1;
}
if (LOBYTE(wsaData.wVersion) != 2 && HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return 2;
}

SOCKET sockPLC = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sockWorker)
{
WSACleanup();
return 3;
}
SOCKADDR_IN addrPLC;
addrPLC.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrPLC.sin_family = AF_INET;
addrPLC.sin_port = htons(9527);

int nRet = 0;
nRet = connect(sockPLC, (SOCKADDR*)&addrPLC, sizeof(SOCKADDR));
if (SOCKET_ERROR == nRet)
{
closesocket(sockPLC);
WSACleanup();
return 4;
}

while (1)
{
char cBufRecv[30] = {0};
recv(sockPLC, cBufRecv, 30, 0);
cout<<cBufRecv<<endl;
Sleep(1000);
}
closesocket(sockPLC);
WSACleanup();
return 0;
}

运行结果:
创建子进程来建立多个client连接 - mihooke - Mihookes blog
 
  评论这张
 
阅读(32)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017