summaryrefslogtreecommitdiff
path: root/code/snippets/socketpair_communication.rb
diff options
context:
space:
mode:
Diffstat (limited to 'code/snippets/socketpair_communication.rb')
-rw-r--r--code/snippets/socketpair_communication.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/code/snippets/socketpair_communication.rb b/code/snippets/socketpair_communication.rb
new file mode 100644
index 0000000..c8e0f09
--- /dev/null
+++ b/code/snippets/socketpair_communication.rb
@@ -0,0 +1,26 @@
+require 'socket'
+
+child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
+maxlen = 1000
+
+fork do
+ parent_socket.close
+
+ 4.times do
+ instruction = child_socket.recv(maxlen)
+ child_socket.send("#{instruction} accomplished!", 0)
+ end
+end
+child_socket.close
+
+2.times do
+ parent_socket.send("Heavy lifting", 0)
+end
+2.times do
+ parent_socket.send("Feather lifting", 0)
+end
+
+4.times do
+ $stdout.puts parent_socket.recv(maxlen)
+end
+