Ruby socket not returning data on second read

206 views Asked by At

I have an asic computer in my house that I don't really have control over, but I can talk to its API over TCP (CGminer OS). I'm trying to record data from it:

socket = TCPSocket.open(address, port)
loop do 
  sleep 1   
  socket.write(command)
  response = socket.read
end

The first iteration of this loop returns the data as expected, the second is an empty string. I'm pretty clueless about sockets and not sure what I need to do. I know I can reopen the socket each iteration if I have to, I'm just hoping I don't need to.

1

There are 1 answers

2
NoobException On

Solution is to just reopen the socket.

loop do
  socket = TCPSocket.open(address, port)
  response = socket.read
  socket.close
end