The EchoServer App in Java

Gabriella’s Journey
3 min readApr 30, 2018

--

The project I’m currently working on is an EchoServer app. In a nutshell, this app connects two machines (a client and a server) together: the client gets an input from the user and sends it to the server for it to output it back to the user.

This projects involves a lot of concepts unknown (or mostly unknown) to me, therefore before starting implementing something I had to make a lot of research in the internet to firstly familiarise with words like host, socket, etc. (this is how I am…)

I started from this documentation which explains well what a socket is, and also provides an example on how to implement a client and a server. Of course, this page wasn’t enough for me to understand everything, so I had to make further (and more detailed) researches.

After all the research I finally managed to put something working together today and implemented a first spike for the project. The next step will be starting all over again and build an app with a decent design, with all concerns nicely and clearly separated and good tests to support it.

But before doing that (this will be tomorrow’s job…) I’m going to explain some basic terminology in this blog post hoping that it could turn out to be helpful for some other 8th Light apprentices in the future who (like me) might not be familiar with the basics. Besides, this will help me to refresh my memory about all the new concepts/definitions I’ve come across in the last days.

First thing: what is a host?

This is the first thing that was confusing me while reading all the documentation about client/server connection and sockets. A host is a computer accessible over a network. It can be a client, or a server, or any type of computer.

This might seem obvious, but it wasn’t for me at the beginning: host and server are two different things. A server is a host, but a host is not necessarily a server. To avoid confusion, servers are often defined as web host or mail host.

The Transmission Control Protocol (TCP)

Client-server applications rely on a lower-level network communication that can be provided by the TCP. TCP is one of the main protocols of the Internet protocol suite that provides a reliable, ordered and error-checked delivery of a stream of bytes between applications running on hosts communicating via an IP network.

To communicate over TCP, a client program and a server program establish a connection to one another via a socket which is bound to their end of the connection. To communicate, the client and the server read and write to the socket bound to the connection.

What is a socket?

A socket is one end-point of a two-way communication link between two programs running on the network. An endpoint is a combination of an IP address and a port number. Every TCP connection can be uniquely identified by its two endpoints.

In Java, Socket class is used to represent the client side of connection whereas ServerSocket class represents the server side of connection.

How does the communication between the programs occur?

Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server listens (aka waits) for a client to make a connection request via the socket.

The client knows the hostname of the machine on which the server is running and the port number on which the server is listening, and use them to make a connection request.

If everything goes well, the server accepts the connection. From the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.

The client and server can now communicate by writing to or reading from their sockets.

Next steps

Here you can see how I wrote the spike for the EchoServer app. It looks quite messy (with all the comments, repetitions, etc.) but it helped me understand the flow behind a client/server connection so I hope you’ll be clement enough :).

Tomorrow I’ll start writing the application from scratch again. I will have to keep the Client and Server in separated classes and write unit tests for them (not stressful at all…).

Also, I’ll have to figure out how to allow the user to keep writing on the terminal by preventing the server and the client from closing the socket somehow, possibly by wrapping the app logic inside a while-loop.

If you have any comments/feedback/suggestions, please feel free to write!

--

--