Chris Egidio
Jun 17 · 3 min read

Architecture

This a very basic TCP server, I kept it simple for brevity. The database is a map structure, and I didn’t use concurrency locking for it. Functions — connection, reader, router and handler are modules and I use composition for them. Input or data from the client sent to the server will be in form — “COMMAND KEY VALUE\r\n”. The “\r\n” signals EOF to the server and will split into a token for the server to handle.

Connection

Function listenAndServe() creates a listener on localhost:8081, running infinitely. And when a new connection is accepted, a go-routine is fired for concurrency.

Reader

Function Reader creates a writer and reader buffer for the connection. Also, it scans the input from the connection and splits at a “\n” returning a token. The token is then passed to the router as a string.

Router

Function router will receive the text and then map — command to handler, passing the data and writer as arguments.

Handler

A handler will compute and store data and write to the client with a response.

Database


Test

Build app

$ go build .

Run app

$ ./app

Now, use a telnet client to talk to the server. The server won’t close connection, so it’s open till you close.

$ telnet 0.0.0.0 8081

And, send a string to the server in a format (Command Key Value\r\n). You don’t have to use a “\r\n” at the end of the input in this case because telnet does that for you.

For example,

$ telnet 0.0.0.0 8081
Trying 0.0.0.0...
Connected to 0.0.0.0.
Escape character is '^]'.
SET boo foo
OK
GET boo
foo
SET key 10000
OK
GET key
10000

XXXVENOM

Software Engineering | Computer Science

Chris Egidio

Written by

Software Engineer | Marine | CS Student

XXXVENOM

XXXVENOM

Software Engineering | Computer Science

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade