tags:

views:

96

answers:

2

Hi,

This is potentially a newbie question, but if i open and write some data to a socket, then exit the subroutine so the socket goes out of scope, and then try and read the data from another program, at a later time, will the data still be there or does it die when the original declarations go out of scope ?

Thanks,

N.

Further information :

I am trying to rewrite 2 programs that use files as the interface to communicate. The general flow is :

Main Process : Write Data.

Main Process : Spawn secondary process(es) onto other nodes in a cluster

Main Process : Wait until Secondary Process finished.

Secondary Process : Read Data (written by main)

Secondary Process : Write Data

Secondary Process : exit

Main Process : Read data.

So i essentially want to replace the Write/Read/Write/Read of files with sockets (which should be much faster!)

+5  A: 

For TCP sockets you need a bi-directional connection opened before sending data, so the question is irrelevant if you don't have a receiving side.

For UDP, if no one is listening on the socket at the time you're sending data, no one will receive it unless you manage to open a listening program fast enough for the data to be still traveling inside the networking drivers. But don't count on it, because the 'localhost loopback' inside the driver shouldn't take more than a few microseconds to deliver the data.

P.S. Perhaps you can get a more suitable answer if you describe your exact situation in more detail. What are you trying to achieve?


Regarding your "further information". You can't do this with sockets by simple replacing the files with sockets and keeping the current scheme. However, you can try to change the scheme by first spawning the child processes and only then send them the data via sockets. When the children finish, they return an answer to the parent via a socket, and exit.

There's an inefficiency here in a sense, because you have to send the same data to each child separately (unless you can use multicasting).

I'm not sure sockets will be much faster than files for you, but they will certainly be safer for more complex scheme and will also allow distribution among machines that don't share a file-system.

Eli Bendersky
+3  A: 

When using a raw socket, if there isn't another endpoint available (connected) at the time that you write the data, the data will be lost. The only way that you could actually write the data without first having connected to the other endpoint would be to use UDP, in which case the data would simply be flushed by the receiving system if no matching endpoint is available.

If you want to have asynchronous delivery you will need to use a message passing system that allows delayed delivery. In this case, the receiver of the message is actually a system process that stores the message until a client requests it. The actual communication takes place between a client on one system and the system process on the other, with the client on the other system obtaining the data locally. You can read more about message passing and its variants at http://en.wikipedia.org/wiki/Message_passing.

tvanfosson