Send data synchronously over multiple TCP Sockets

Can I pick peoples brains.

I have a script that opens six tcp client sockets all of which then sit
there waiting to receive a trigger that tells them to send data. It’s
time critical so sending consecutively makes having six sockets
pointless as it’s first come first served.

Is it possible to send data to all six sockets at exactly
the same time?

This is essentially how my code works at the moment.

6.times do |i|
@connection[i] = TCPSocket.new(server, port)
end

Thread.new(data) do
if data = true
@connection.map { |i|
i.send_data
end
end

Thanks

Sean W. wrote in post #1181839:

Can I pick peoples brains.

I have a script that opens six tcp client sockets all of which then sit
there waiting to receive a trigger that tells them to send data. It’s
time critical so sending consecutively makes having six sockets
pointless as it’s first come first served.

Is it possible to send data to all six sockets at exactly
the same time?

writing data to socket mean a transfer between your process memory
to TCP buffer ( usually 64 KB ). So if buffers are free, you can send
in a loop.

TCP was build in idea of file transfer, it is not the good media
for realtime stream.

for more efficient in timing precision, you can use:

  • UDP datagramme
  • UDP Multicast : you send one datagram, and then
    switchs/router will duplicate it
  • raw socket
  • libpcap

Regis d’Aubarede wrote in post #1181855:

writing data to socket mean a transfer between your process memory
to TCP buffer ( usually 64 KB ). So if buffers are free, you can send
in a loop.

TCP was build in idea of file transfer, it is not the good media
for realtime stream.

for more efficient in timing precision, you can use:

  • UDP datagramme
  • UDP Multicast : you send one datagram, and then
    switchs/router will duplicate it
  • raw socket
  • libpcap

Thanks for the answer but I think you misunderstand my question. I have
an array of six sockets open in six threads and I can send the data to
each but the problem is they’re sent asynchronously.

The data is actually an XML request and I’ve already sent all but the
last “>” character and when a condition becomes true, I send that
character to each of the six sockets I have open and primed.

So it’s essentially doing the following loop …

Send “>” via socket 1
end
Send “>” via socket 2
end
Send “>” via socket 3
end
Send “>” via socket 4
end
Send “>” via socket 5
end
Send “>” via socket 6
end

That works but the time taken to send the data via socket 1 in 200ms
whilst by the time the data has been sent via socket 6 the time elapsed
is nearly 1 second.

I need the data to all six sockets in 200ms i.e. synchronously rather
than asynchronously.

Thanks again

Sean W. wrote in post #1181858:

That works but the time taken to send the data via socket 1 in 200ms
whilst by the time the data has been sent via socket 6 the time elapsed
is nearly 1 second.

I need the data to all six sockets in 200ms i.e. synchronously rather
than asynchronously.

1 seconds !!! it is really too much
What is your environment ? ESP8266 with wifi ? :slight_smile:

in attachment, a little test (with minitcp, i am lazy…)
I got 8 ms for emissions of the last byte.

You should read/write on socket with send/recv, not print/puts.

If i replace socket.send(data,0) by socket.print(data) , i got issues
I think that puts/print use internal buffer, which may trouble your
process.

( You can also use soclet.flush(), which flush the internal buffer to io
port )

and I forget socket options:

  • no-ndelay : disable Nagle algorithm
  • keepalive

( minitcp use them implicitly )

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs