Share socket fd between processes

Hi
Can i share socket fd's between process. Is it a good coding style.
thanks in advance
Anoop
[121 byte] By [anoop_xp] at [2007-11-20 1:37:52]
# 1 Re: Share socket fd between processes
sure u can do that, and it is just one of the programmign techniques.

If it is good or bad, depends on what do u want to do ...

tell us some thing about the scenario and then we can tell u ... :wave:
salman108 at 2007-11-9 13:53:56 >
# 2 Re: Share socket fd between processes
Hi salman,

It was a client server application, where one process acts as a server which listens on port 138 and the other is a client process on the same machine which gets the packet from the server process thru a pipe and replies to the remote client using a different port with a different socket fd.

But my need is to send the reply thru port 138 instead of a non-standard port, in this case i have to share the socket fd between the client and server processes. Sio that the client process can also send thru port 138

Can u please tell me is there a way to share it on a unix machine.

Thanks in advance
Anoop
anoop_xp at 2007-11-9 13:54:56 >
# 3 Re: Share socket fd between processes
Any body help on this...
anoop_xp at 2007-11-9 13:55:56 >
# 4 Re: Share socket fd between processes
No Anoop, sharing a socket among proceses is not a good idea, I believe the socket handle is process specific, you need to create a socket and use it in that process only.

Can you descibe why do you need to share socket with some example.
Krishnaa at 2007-11-9 13:57:02 >
# 5 Re: Share socket fd between processes
A socket handle is a handle and can be duplicated across process boundaries like any other handle. See "Passing Sockets Between Processes" at http://tangentsoft.net/wskfaq/articles/passing-sockets.html

For sockets, Winsock2 has a special function for such purposes. You can duplicate sockets across process boundaries using WSADuplicateSocket().

But why would you want to do this?

Mike
MikeAThon at 2007-11-9 13:58:01 >
# 6 Re: Share socket fd between processes
Thanks fo your replies,

The problem is iam doing it on a unix machine, if iam doing it on windows i can use WSADuplicateSocket.

The scenario is
I have process 1 which is sending datagrams to client from port "x". The client will send reply to port 138, where another process 2 is listening.
But the problem is the client is sending to port x where im not listening.

Any ideas on this...

Thanks
Anoop
anoop_xp at 2007-11-9 13:59:00 >
# 7 Re: Share socket fd between processes
Late reply !! :blush:

Anoop are u doing it on TCP ... I think u are,

I dont understand what u mean when u say, it sends packets when u are not listening !!! socket alerts are implemented as call back functionc and we dont have to check the status our selves ....

What i can suggest is that, in ur server application u can make a thread which keeps listenign on port 138 and when ever u get a connect request or what ever message u just pass the appropriate data to a background worker thread while ur listening thread keeps listening.

This is what i meant when u replied that a single socket descriptor can be shared amongst different processes. sorry if I was ambigious. I agree with Krishnaa, it is bad technique to share amongst Processes. It is ok if u do it with threads in a same process.

Anoop can u plz say more abt this application, because for now I can't understand the scenario ... I think u are implementing a different solution then to what is needed.
salman108 at 2007-11-9 14:00:05 >