Pass an open SOCKET to a new process?
- From: "Mike Cooper" <MikeCooper@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 17 Jun 2005 13:25:04 -0700
I've got a WinSock server process which accepts a socket connection and then
needs to create a new process to server the client connection. How do I pass
on the SOCKET connection itself to the new process?
Here's some psuedo code on what I'm trying to do:
SockFd = socket(AF_INET, SOCK_STREAM, 0);
memset(&ServAddr, 0, sizeof(ServAddr));
ServAddr.sin_family = AF_INET;
ServAddr.sin_addr.s_addr = Addr;
ServAddr.sin_port = Port;
bind(SockFd, (struct sockaddr *) &ServAddr, sizeof(ServAddr));
listen(SockFd, 5);
sock = accept(SockFd, ...);
CreateProcess(...);
I need to somehow pass the client connection on "sock" on to the new process
so that the new process can handle all the client's requests, then exit.
I'm using threads to wrap the polling and the client CreateProcess portions
in. I would like to avoid a pure thread model for client processing because
client's do not connection often, but the server is expected to run for many
months at a time without a restart. A seperate process seems like the most
stable way to go.
This is a port of a server from UNIX land. The UNIX code simply does a
fork() which allows the child process to inherit the socket connection from
the client.
I've been trying to see if there's some way of turning the SOCKET into a
HANDLE and then duplicating that so the new process gets the HANDLE, but
there doesn't seem to be a portable way of converting SOCKET to HANDLE.
Several folks have told me that a SOCKET value *is* a HANDLE value, but it's
not written this way and could change with no warning.
Any pointers would be most appreciated.
-mike
.
- Follow-Ups:
- Re: Pass an open SOCKET to a new process?
- From: Eugene Gershnik
- Re: Pass an open SOCKET to a new process?
- Prev by Date: Re: Improving this code
- Next by Date: Re: Problem with the send() and recv() in sockets
- Previous by thread: SNMP Get Reponse Question
- Next by thread: Re: Pass an open SOCKET to a new process?
- Index(es):
Relevant Pages
|