TCPSocket connection reset by peer

1k views Asked by At
require 'socket'
socket = TCPSocket.open('stream-api.betfair.com', '443') 
while line = socket.gets
  puts line.chop
end
socket.close

I should receive something like {"op":"connection","connectionId":"002-230915140112-174"}

but I receive Connection reset by peer which

means the remote end would have sent a reset packet (RST) to kill the connection without an orderly shutdown (close). In that case you know it was the peer(client).

betfair included a nodejs example and also csharp/java examples

Any help is much appreciated. Thanks!

1

There are 1 answers

0
Atille On BEST ANSWER

First thing, you should replace the string '443' by an integer :

TCPSocket.open('stream-api.betfair.com', 443)

Anyway, it seems to be related with the SSL negociation : the following Stackoverflow post gives a quick idea about what would work : How to establish a SSL enabled TCP/IP Connection in Ruby. Using this method, I works.

require 'socket'
require 'openssl'

host = 'stream-api.betfair.com'
port = 443

socket = TCPSocket.open(host,port)
ssl_context = OpenSSL::SSL::SSLContext.new()
ssl_context.ssl_version = :SSLv23
ssl_socket = OpenSSL::SSL::SSLSocket.new(socket, ssl_context)
ssl_socket.sync_close = true
ssl_socket.connect
while line = ssl_socket.gets
  p line
end
ssl_socket.close

Result :

"{\"op\":\"connection\",\"connectionId\":\"001-151118094105-259478\"}\r\n"

Dealing with SSL/TLS protected connection is sometime quite verbose with Ruby. In the example you gave, in NodeJS, the hint is the first line :

var tls = require('tls');