2

I am writing a socket program which consists of a bunch of slave processes that will be sitting on each machine in a cluster of computers, while a master process instructs them to move local files over to remote slaves on remote nodes. The primary task of these slave processes is to read files off their local hard disks and transfer them to other slaves on other machines. I want to have this functionality of both listening for file data and sending over file data built into a single process.

Is it possible that I can have both the sending and the receiving bits in the same process ?

//I want this to send a connect() request to every other slave node
initialize_Connections();

//Have an accept() call for accepting the connection requests from the other nodes
accept_Connections();

Is it even possible to pull off something like this ? I looked at forking the process between the initialize_Connections() and the accept_Connections() call (ie a child process calls the initialize_Connections() and the parent takes care of the accept_Connections()) but that did not work out for some mysterious (to me) reason.

Is it possible to use nonblocking connect() and accept() in this situation ?

Arjun J Rao
  • 925
  • 1
  • 10
  • 25

4 Answers4

0

using threads is not mandatory, you just need to setup one listening socket that fires a socket for each incoming connection and a bunch of sockets for the connections versus the other clients, and pool/select on every socket for events...

Leonardo Bernardini
  • 1,076
  • 13
  • 23
0

Is it possible that I can have both the sending and the receiving bits in the same process? Is it possible to use nonblocking connect() and accept() in this situation ?

Yes, absolutely. You can do it with multiple threads, or with just a single thread using select() or poll() to multiplex non-blocking I/O across multiple sockets.

Neither of these approaches is trivial to get 100% right, but they are quite doable. If you'd like to avoid spending a bunch of time learning (and debugging) multiple-connection socket programming details, I'd recommend using some middleware that does this sort of thing for you, rather than rolling your own. I'm partial to my own library that I wrote to handle this sort of thing, but there are other good ones out there as well.

Community
  • 1
  • 1
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

The select() call allows you service multiple sockets without entering blocking calls. It requires you to set up some structures that get populated by select(). You call select() iteratively, examining the structures to see if reading &/or writing to one of the specified sockets would succeed immediately. Then, you can call read() or write() on an open socket without fear that it may block indefinitely.

theNbomr
  • 124
  • 3
-1

I would have each slave process execute on two permanent threads. On one thread, the process would connect to the master process and then receive instructions on which file to send to which other slave processes. For each other slave process in an instruction, this thread would create a temporary thread to send the file. On the other permanent thread, the slave process would receive connections from other slave process. For each connection it would create a temporary thread on which the file was received.

Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59