# SSLSocket -- Connection reset by peer

**URL:** https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115
**Category:** ruby-talk
**Created:** [19 September 2010 22:59 UTC](https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115 "2010-09-19T22:59:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mike\_Vastola](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@Mike\_Vastola](https://rubytalk.org/u/Mike_Vastola)
#### Post date: [19 September 2010 22:59 UTC](https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115/1 "2010-09-19T22:59:26Z")

</div>

Hi Guys,  
I'm having a weird (to me, at least) problem trying to connect to an SSL  
socket using a custom protocol and read and send some data. Any help  
would be greatly appreciated.

I'm getting the error:  
/usr/lib/ruby/1.8/openssl/buffering.rb:35:in `sysread': Connection reset  
by peer (Errno::ECONNRESET)  
&nbsp;&nbsp;from /usr/lib/ruby/1.8/openssl/buffering.rb:35:in `fill\_rbuff'  
&nbsp;&nbsp;from /usr/lib/ruby/1.8/openssl/buffering.rb:67:in `read'  
&nbsp;&nbsp;from ./display\_pusher.rb:23:in `update\_display'  
&nbsp;&nbsp;from ./display\_pusher.rb:44

My code is as follows:

> **···**
>
> -------------  
> #!/usr/bin/ruby
> 
> require 'openssl'  
> require 'socket'  
> require 'resolv'
> 
> class DisplayUpdater
> 
> &nbsp;&nbsp;def self.update\_display(address, port, data)  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx = OpenSSL::SSL::SSLContext.new()  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx.ca\_file = 'cacert.pem'  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx.cert = OpenSSL::X509::Certificate.new(File.read("cert.pem"))  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx.key = OpenSSL::PKey::RSA.new(File.read("key.pem"))  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx.verify\_mode = OpenSSL::SSL::VERIFY\_PEER  
> &nbsp;&nbsp;&nbsp;&nbsp;tcpSock = TCPSocket::new(address, port)  
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock = OpenSSL::SSL::SSLSocket.new(tcpSock, sslCtx)  
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock.sync\_close = true  
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock.connect
> 
> &nbsp;&nbsp;&nbsp;&nbsp;success = false  
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock.write(data + "\n")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;response = sslSock.read #\*\*ERROR OCCURS HERE
> 
> &nbsp;&nbsp;&nbsp;&nbsp;#do some stuff with response here
> 
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Success!"
> 
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock.close  
> &nbsp;&nbsp;&nbsp;&nbsp;sslCtx.flush\_sessions  
> &nbsp;&nbsp;&nbsp;&nbsp;return success  
> &nbsp;&nbsp;end
> 
> end
> 
> DisplayUpdater::update\_display('127.0.0.1', 23, '--')
> 
> -----------
> 
> The server is running xinetd -\> stunnel -\> a custom c++ app, and I can  
> connect to it flawlessly from the same computer that's running ruby with  
> openssl s\_client.
> 
> Any ideas what I'm doing wrong?
> 
> Thanks!  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)
#### Post date: [20 September 2010 08:41 UTC](https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115/2 "2010-09-20T08:41:15Z")

</div>

Mike Vastola wrote:

> DisplayUpdater::update\_display('127.0.0.1', 23, '--')

You're really running an SSL server on the telnet port on your local  
computer?

> I can  
> connect to it flawlessly from the same computer that's running ruby with  
> openssl s\_client.

Can you show the full command line you use for openssl s\_client ? And  
the certificate validation result when you run it?

Also, have you tried:  
sslSock.write(data + "\r\n")

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Mike\_Vastola](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@Mike\_Vastola](https://rubytalk.org/u/Mike_Vastola)
#### Post date: [20 September 2010 14:19 UTC](https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115/3 "2010-09-20T14:19:27Z")

</div>

Brian Candler wrote:

> Mike Vastola wrote:
> 
> > DisplayUpdater::update\_display('127.0.0.1', 23, '--')
> 
> You're really running an SSL server on the telnet port on your local  
> computer?

Haha. No. I change the port/hostname to mask what it really was.

> > I can  
> > connect to it flawlessly from the same computer that's running ruby with  
> > openssl s\_client.
> 
> Can you show the full command line you use for openssl s\_client ? And  
> the certificate validation result when you run it?

openssl s\_client -connect {non-localhost-host}:{non-telnet-port} -cert  
cert.pem -key key.pem -CAfile cacert.pem

CONNECTED(00000003)  
depth=1 {INSERT\_CA\_SUBJECT\_HERE}  
verify return:1  
depth=0 {INSERT\_SERVER\_SUBJECT\_HERE}  
verify return:1

> **···**
>
> ---  
> Certificate chain  
> 0 s: {INSERT\_SERVER\_SUBJECT\_HERE}  
> &nbsp;&nbsp;&nbsp;i: {INSERT\_CA\_SUBJECT\_HERE}  
> 1 s: {INSERT\_CA\_SUBJECT\_HERE}  
> &nbsp;&nbsp;&nbsp;i: {INSERT\_CA\_SUBJECT\_HERE}  
> ---  
> Server certificate  
> -----BEGIN CERTIFICATE-----  
> {INSERT\_CERTIFICATE\_HERE}  
> -----END CERTIFICATE-----  
> subject= {INSERT\_SERVER\_SUBJECT\_HERE}  
> issuer= {INSERT\_CA\_SUBJECT\_HERE}  
> ---  
> Acceptable client certificate CA names  
> {INSERT\_CA\_SUBJECT\_HERE}  
> ---  
> SSL handshake has read 4252 bytes and written 5147 bytes  
> ---  
> New, TLSv1/SSLv3, Cipher is AES256-SHA  
> Server public key is 4096 bit  
> Compression: NONE  
> Expansion: NONE  
> SSL-Session:  
> &nbsp;&nbsp;&nbsp;&nbsp;Protocol : TLSv1  
> &nbsp;&nbsp;&nbsp;&nbsp;Cipher : AES256-SHA  
> &nbsp;&nbsp;&nbsp;&nbsp;Session-ID:  
> 1BE2DD87165574CD6F2D99720007FDCA811C63546FB449A72B0293C54177A5E5  
> &nbsp;&nbsp;&nbsp;&nbsp;Session-ID-ctx:  
> &nbsp;&nbsp;&nbsp;&nbsp;Master-Key:  
> BCCAE579F3AF185BDAFF1D30D6F058573EC8266DE2877CE73E30ED7ED2BE819DD15B7098304F59529BAF6BE12FD18EED  
> &nbsp;&nbsp;&nbsp;&nbsp;Key-Arg : None  
> &nbsp;&nbsp;&nbsp;&nbsp;Start Time: 1284991918  
> &nbsp;&nbsp;&nbsp;&nbsp;Timeout : 300 (sec)  
> &nbsp;&nbsp;&nbsp;&nbsp;Verify return code: 0 (ok)  
> ---  
> %%% Starting Here Is the actual Custom Protocol Communications %%%  
> --  
> OK  
> \*\*\* Setting display 00 to value '--' via /dev/ttyUSB0.  
> DONE  
> %%% End Custom Protocol Communications %%%  
> closed
> 
> > Also, have you tried:  
> > sslSock.write(data + "\r\n")
> 
> No.. will try though..  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Mike\_Vastola](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@Mike\_Vastola](https://rubytalk.org/u/Mike_Vastola)
#### Post date: [20 September 2010 14:27 UTC](https://rubytalk.org/t/sslsocket-connection-reset-by-peer/60115/4 "2010-09-20T14:27:22Z")

</div>

Whoa. Just got it working when I changed the code to:

> **···**
>
> --------  
> &nbsp;&nbsp;&nbsp;&nbsp;sslSock.write(data + "\n")
> 
> &nbsp;&nbsp;&nbsp;&nbsp;while line = sslSock.gets  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts line  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success = true if line.chop == "DONE"  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> --------
> 
> No idea why/how that fixed it, but thanks a ton!!!  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
