{{announcement.body}}
{{announcement.title}}

Docker to Docker Networking Between TCP Client and Server

DZone 's Guide to

Docker to Docker Networking Between TCP Client and Server

This article presents a step-by-step instructions on how to create a TCP client and server for IoT applications using Docker containers and networking.

· Cloud Zone ·
Free Resource

telephone

Take a look at how two containers communicate

A TCP-based client and server are a frequently needed setup in IoT-based applications.

Presented in this article are:

  1. A method to create a Java-based client and server to send and receive binary data
  2. The client and server, each run inside a Docker container and communicating through Docker networking.

You may also enjoy: Understanding Docker Networking

Create a TCPClient.java as per the code provided below

Java







Create a manifest file for the TCPClient as is shown below:

Java







Create a Dockerfile for TCPClient as below:

Dockerfile







Create a TCPServer.java as per the code provided below:

Java







Create a manifest file for TCPServer as below:

Shell







Create a Dockerfile for TCPServer as below

Shell
 






The directory structure should be as displayed as shown below:

Directory structure

   
Run the following commands at the unix prompt:

Shell
 







Create the network:

Shell







Run the server:

Shell







Run the client:

Shell
 






 

You should see the client output as shown below:

Client output


And the server output should be as shown below:

Server output



Topics:
containers, docker, docker networking, networking, tcp, tcp protocol, tcp server

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}