Click to See Complete Forum and Search --> : How to pass TCP socket handle been process?


swathi_k
July 18th, 2007, 10:28 AM
( C++ in VC++6.0 IDE )
In MasterProcess I created pipe(name pipe or anonymous pipe) and spawn process( slaveProvess). then I crated Tcp socket,When ever it accepted new TCP socket I want to send this accepted socket handle to slaveprocess.exe through pipe then slaveprocess read accepted socket handle from pipe and do socket read operations .
But at slaveProcess socket read giving error
10038 An operation was attempted on something that is not a socket.

So how to pass socket handle between process

Here is basic code

in masterProcess.exe

hPipe=CreateNamedPipe(\\\\.\\Pipe\\MyNamedPipe)
CreateProcess(slaveProcess.exe)
ConnectNamedPipe(hPipe)
Create TCP socket
Bind
listen
while(1)
{

Acceptfd=accept()
_itoa(Acceptfd, szBuffer, 10 );//< is this correct way to do
WriteFile(hPipe, szBuffer)

}

In slaveProcess.exe

hPipe = CreateFile(\\\\.\\Pipe\\MyNamedPipe)
ReadFile(hPipe ,buffer)
SOCKET socketfd= (SOCKET)atoi(buffer); );//< is this correct way to do
int ret1=recv(sockfd,serverbuff,1024,0);
int err=WSAGetLastError();

o/p ret1=-1 err=10038

MikeAThon
July 18th, 2007, 01:22 PM
For Winsock, look at WSADuplicateSocket(). According to the docs:

"The WSADuplicateSocket function is used to enable socket sharing between processes. A source process calls WSADuplicateSocket to obtain a special WSAPROTOCOL_INFO structure. It uses some interprocess communications (IPC) mechanism to pass the contents of this structure to a target process, which in turn uses it in a call to WSASocket to obtain a descriptor for the duplicated socket. The special WSAPROTOCOL_INFO structure can only be used once by the target process."

http://msdn2.microsoft.com/en-us/library/ms741565.aspx

I have never used it, so I can't tell you any more.

Mike