small medium large xlarge

Working with TCP Sockets


Cover image for Working with TCP Sockets

Working with TCP Sockets


Do you know how your web server opens a socket, binds to an address, and accepts a connection? I did a lot of Rails development before I understood the mechanics of the network stack I was working on top of. Forget those 1,000+ page networking manuals; Working With TCP Sockets zeroes in on exactly what you need to know as a Ruby developer working on the web today. After reading the book, you’ll have a good understanding of socket programming basics—enough to write servers, client libraries, or concurrent network programs.

This work was written and produced entirely by the author. We are proud to be distributing it.

About this Title

Pages: 185
Published: 2013-01-18
Release: P1.0 (2013-01-18)
ISBN: pending

The book is organized to start with the lowest-level concepts, then work up to the high-level Ruby wrappers, and finally up to more complex stuff. In the beginning you’ll find out how to create sockets using the lowest-level calls that Ruby offers. Following that, you’ll see the bind/listen/accept or bind/connect lifecycle methods for servers and clients, respectively. You’ll then learn about the various high-level wrapper classes that Ruby provides so you don’t have to worry about the boilerplate details.

The middle section of the book is all about more advanced topics such as properly implementing socket timeouts, SSL sockets, buffering, and more. The last section puts all the abstract stuff into practice by building an FTP server several times over using different concurrency patterns.

All the code in the book is written in Ruby, but this stuff doesn’t just apply to Ruby. The Berkeley Sockets API has been in use for more than 25 years and has bindings in any modern programming language. You can learn the fundamentals in Ruby, then take them with you when you’re programming in Python, Go, C, or whatever. This is portable knowledge that will serve you for many years to come.

This book was written and produced entirely by the author. We are proud to be distributing it.

What You Need

The code samples in the book are written against Ruby 1.9.3 and assume a working knowledge of Ruby.

Contents & Extracts


Your First Socket

Establishing Connections

Server Lifecycle

Client Lifecycle

Exchanging Data

Sockets Can Read

Sockets Can Write


Our First Client/Server

Socket Options

Non-blocking IO

Multiplexing Connections

Nagle’s algorithm

Framing Messages


DNS Lookups

SSL Sockets

Urgent Data

Network Architecture Patterns


Process per connection

Thread per connection


Thread Pool

Evented (Reactor)


Closing Thoughts


My name is Jesse Storimer. I’m a programmer and a tinkerer. For the past four years I’ve been solving hard problems at Shopify, one of the largest, busiest Ruby on Rails sites on the web. My journey into the world of socket programming started while working on web infrastructure and hasn’t slowed down yet. I live in rural Ontario, Canada with my wife and daughter. And, yes, I’m working on my Unix beard. I also write at