261. Writing a blocking TCP server/client application
In this problem, we will write a blocking TCP server/client application. More precisely, let’s start with a single-thread blocking TCP echo server.
Writing a single-thread blocking TCP echo server
In order to write a single-thread blocking TCP echo server, we will follow these steps:
- Create a new server socket channel
- Configure the blocking mechanism
- Set the server socket channel options
- Bind the server socket channel
- Accept connections
- Transmit data over a connection
- Close the channel
So, let’s start with the first step.
Creating a new server socket channel
Creating and opening a new server socket channel (stream-oriented listening socket) can be done via the thread-safe java.nio.channels.ServerSocketChannel
API as follows:
ServerSocketChannel serverSC = ServerSocketChannel.open();
The resulting server socket channel is not bound/connected...