In my journey of learning Crystal language, I want to record notes and left guide on how to use tools. I started with understanding how TCPServer and TCPScoket are working and met some unexpected for my behavior. Today I am gonna discuss how to run a sample server application, debug packets with Wireshark and find better way write code (at least I think so).

Open the port

The first step is to open the port and accept connections. I found a sample TCP server from the official documentation. A bit of tuning and logging:

# server.cr

require "socket" def handle_client(client)

puts "Connected client: #{client}"

while message = client.gets

puts "[#{client}] Message: #{message}"

client.puts message

end

end server = TCPServer.new("localhost", 3000)

while client = server.accept?

spawn handle_client(client)

end

Run server application:

$ crystal server.cr

In another terminal run a client, that connects to the port 3000 :

$ nc localhost 3000

hello

hello

world

world

Amazing. A small example with a lot of possibilities. From this point, it could be upgraded to a simple chat server, remote execution of commands and etc.

TCP network analyzer

I would like to know, how TCP is working on a new connection and try to understand all this Voodoo. From previous years, I heard about tcpdump and wireshark tools for this needs. I’d like to play with wireshark .

$ brew cask install wireshark