How to Create a simple TCP Client-Server Connection in Java?

TCP can be defined as Transmission Control Protocol. This is the standard protocol for transmitting data over a network. It provides reliable, structured, and error-checked data delivery between applications running on hosts connected via an IP (Internet Protocol) network. In Java, we can create TCP client-server connections using the Socket and ServerSocket classes from the java.net package .

In this article, we will learn how to create a simple TCP client-server connection in Java.

TCP client-server connection

Key Terminologies

Step-by-Step implementation of Server-Side to Create Connection

Implementation:

Output:

Before client connected to the server:

Server is running and waiting for client connection. 

After client connected:

Server side Output

Step-by-Step implementation of Client-Side to Create Connection

Implementation:

Output:

Below we can see the output received by the client in console.

Client side Output

In the above example, we have created a TCP connection between client and server by sending and receiving data between client and server on port 9090.