summaryrefslogtreecommitdiff
path: root/code/snippets/socketpair_communication.rb
blob: c8e0f09ed4c04aa8d4d283ac018b4345b397a64b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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