安装SPI 基础服务后导致socket在2个进程中无法互用。就是在A进程中创建的SOCKET.通过WSADuplicateSocket 获取的WSAPROTOCOL_INFOW信息发送到进程B中。进程B使用WSAPROTOCOL_INFOW 创建套接字会返回失败。错误码是10022.参数无效。不装SPI 基础服务这代码是没问题的。 求解。下面附上2个进程的代码。
//////////////////////////////////////进程A//////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA wsaData;
WSAStartup(0x202, &wsaData);
SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,NULL,0,WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET)
{
printf("WSASocket 失败:%d.\r\n",WSAGetLastError());
goto end;
}
printf("WSASocket 成功.Socket = %d.\r\n",s);
sockaddr_in service ={0};
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;//inet_addr("192.168.2.36");
service.sin_port = htons(2000);
//----------------------
// Bind the socket.
int iResult = bind(s, (SOCKADDR *) &service, sizeof (service));
if (iResult == SOCKET_ERROR) {
printf("bind 失败:%d.\r\n",WSAGetLastError());
closesocket(s);
WSACleanup();
goto end;
}
else
printf("bind success.\r\n");
if (SOCKET_ERROR == listen(s,5))
{
printf("bind 失败:%d.\r\n",WSAGetLastError());
closesocket(s);
WSACleanup();
goto end;
}
else
printf("listen success.\r\n");
TCHAR path[MAX_PATH] =_T("C:\\TestWSADuplicateSocket.exe");
STARTUPINFO StartupInfo ={0};
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.lpReserved=NULL;
StartupInfo.lpDesktop=NULL;
StartupInfo.lpTitle=NULL;
StartupInfo.dwX=0;
StartupInfo.dwY=0;
StartupInfo.dwXSize=0;
StartupInfo.dwYSize=0;
StartupInfo.dwXCountChars=500;
StartupInfo.dwYCountChars=500;
StartupInfo.dwFlags=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow=SW_SHOWNORMAL;
StartupInfo.cbReserved2=0;
StartupInfo.lpReserved2=NULL;
StartupInfo.hStdInput=stdin;
StartupInfo.hStdOutput=stdout;
StartupInfo.hStdError=stderr;
PROCESS_INFORMATION processInfo={0};
if(!CreateProcess(NULL,path,FALSE,NULL,FALSE,CREATE_NEW_CONSOLE ,NULL,NULL,&StartupInfo,&processInfo))
{
printf("CreateProcess 失败:%d.\r\n",GetLastError());
goto end;
}
else
printf("CreateProcess success.\r\n");
WSAPROTOCOL_INFOW ProtocolInfo= {0};
iResult = WSADuplicateSocket(s,processInfo.dwProcessId,&ProtocolInfo);
if(iResult != 0)
printf("WSPDuplicateSocket Error :%d", iResult);
else
printf("WSPDuplicateSocket success.\r\n");
CPolicyMgrShare::GetInstance()->Init();
CPolicyMgrShare::GetInstance()->WirteBuf((char*)&ProtocolInfo,sizeof(WSAPROTOCOL_INFOW));
printf("wait..\r\n");
WaitForSingleObject(processInfo.hProcess,INFINITE);
CloseHandle(processInfo.hProcess);
printf("Create process exit..\r\n");
end:
getchar();
return 0;
}
//////////////////////////////////////进程B////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
MessageBox(NULL,_T("aaa"),_T("bbb"),MB_OK);
WSADATA wsaData;
if (WSAStartup(0x202, &wsaData) != 0)
{
printf("WSAStartup 失败:%d.\r\n",WSAGetLastError());
goto end;
}
WSAPROTOCOL_INFO LocalProtocolInfo;
CPolicyMgrShare::GetInstance()->Init();
CPolicyMgrShare::GetInstance()->ReadBuf((char*)&LocalProtocolInfo,sizeof(WSAPROTOCOL_INFO));
SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,&LocalProtocolInfo,0,WSA_FLAG_OVERLAPPED);
//SOCKET s = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP,NULL,0,WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET)
{
printf("WSASocket 失败:%d.\r\n",WSAGetLastError());
goto end;
}
else
printf("WSASocket success socket = %d.\r\n",s);
while (1)
{
sockaddr_in local ={0};
int nLen = sizeof(sockaddr_in);
SOCKET saccess = accept(s,(sockaddr*)&local,&nLen);
if (saccess == INVALID_SOCKET)
{
printf("accept INVALID_SOCKET.\r\n");
Sleep(1000);
}
else
{
printf("accept success socket = %d.\r\n",saccess);
}
}
end:
WSACleanup( );
getchar();
return 0;
}
这个BUG 我搞了1天没任何头绪。希望谁能给点提示!
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课