# Is there a way to open a TCP Socket with a try/catch structure in case of timeouts?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-open-a-tcp-socket-with-a-try-catch-structure-in-case-of-timeouts/260474>\
**Category:** Uncategorized\
**Created:** [November 11, 2020, 7:23pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-open-a-tcp-socket-with-a-try-catch-structure-in-case-of-timeouts/260474 "2020-11-11T19:23:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![redapplesonly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8797f3/32.png) [@redapplesonly](https://www.ruby-forum.com/u/redapplesonly)\
**Post date:** [November 11, 2020, 7:23pm UTC](https://www.ruby-forum.com/t/is-there-a-way-to-open-a-tcp-socket-with-a-try-catch-structure-in-case-of-timeouts/260474/1 "2020-11-11T19:23:43Z")

</div>

Hi everyone,

I’m a programmer who is also a beginner in Ruby. I’ve inherited a Ruby program from another colleague, and I need to figure out the syntax to safely modify the original code.

The original code accepts a data record (a linked list, perhaps, I’m not sure) from an external source. The code then opens a socket on TCP 12345, sends the entire record, and then listens to the socket for a response from the remote host. The remote guy does some analysis and then sends back a string. The Ruby code then takes that string and appends it to the data record in a new field called “`new_information`”. Here’s the code:

```
require 'socket'
...accept data record "event"...
socket = TCPSocket.new("192.168.3.1", 12345)
socket.write (event.to_hash).to_s
response = socket.recv(1000)
socket.close
event.set("new_information", response)

```

All of this works flawlessly. (For those who are curious, this is part of a Logstash implementation. Logstash allows Ruby code to help process incoming log records.)

But now I’m worried about the scenario where the remote host is down and unavailable. I need to include an if/else or try/catch structure so that if the Ruby socket times out and no answer is ever received, the “`new_information`” field can still be populated with something meaningful. Here’s my stab at the modified code:

```
require 'socket'
...accept data record "event"...
begin
   socket = TCPSocket.new("192.168.3.1", 12345)
   socket.write (event.to_hash).to_s
   response = socket.recv(1000)
   socket.close
   event.set("new_information", response)
rescue Errno::ETIMEDOUT
   p 'timeout'
   event.set("new_information", "remote_svr_down!")
end

```

As you can probably tell, I don’t really know what I’m doing. Trouble is, the Logstash system with which I am working does not allow you to see the precise syntax error when the code crashes. So I have no idea if my modified code is nearly correct or way, way off.

Another question I have is… how long is the socket timeout here? I assume it is the default value. Is there a way to set that?

FULL DISCLOSURE :: I have also [posted this question here](https://www.reddit.com/r/ruby/comments/jsemk2/ruby_how_to_add_a_trycatch_clause_to_a_tcp_socket/).

---

<div class="post-metadata">

**Author:** ![SouravGoswami](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/souravgoswami/32/2543_2.png) [@SouravGoswami](https://www.ruby-forum.com/u/SouravGoswami)\
**Post date:** [November 12, 2020, 5:21am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-open-a-tcp-socket-with-a-try-catch-structure-in-case-of-timeouts/260474/2 "2020-11-12T05:21:19Z")

</div>

Are you talking about this?

 ![image](https://www.ruby-forum.com/uploads/default/original/2X/a/a2e2ed37fbc204a8030b09b7162f740996288bbd.png)

Code (getting error even with header):

```auto
#!/usr/bin/ruby -w
require 'socket'

server = TCPServer.new 2000

while true
	client = server.accept
	client.puts %Q<HTTP/1.1 200\r\nContent-Type: text/html;charset="utf-8"\r\nContent-Length: 1200\r\ncontent-encoding:gzip\r\n\r\n>
	client.puts IO.read('p.html.gz')
	client.close
end

```

If yes, I suggest strongly use WEBRick or perhaps Unicorn:

> **[RubyDoc.info: 
      
  Class: Unicorn::HttpServer
  
    – Documentation for...](https://www.rubydoc.info/gems/unicorn/2.0.0/Unicorn/HttpServer)**
>
> Documenting RubyGems, Stdlib, and GitHub Projects

Or maybe Puma:

> **[GitHub - puma/puma: A Ruby/Rack web server built for parallelism](https://github.com/puma/puma)**
>
> A Ruby/Rack web server built for parallelism
