Java networking Examples: Client and server implementation in TCP

 TCP (Transmission Control Protocol) is a transport layer protocol which deals with end to end transmission. It makes a reliable connection.

Let us create a client server program using TCP.

Program implementation:

Server:

  • The implementation starts with including the built in packages. The steps are given below.
  • A public class is created with main() function.
  • A ServerSocket object is created with the port 12345.
  • Clientsocket object is derived from Socket. The object calls the accept() function to accept the clientsocket.
  • ‘getInetAddress()’ is used to get the internet Address.
  • ‘getOutputStream()’ is used to get the output stream from the socket.
  • ‘PrintWriter’ object writer1 gets the outputStream as a input.
  • Print the output as response from server.
  • Close() the socket.

Server Program:

import java.net.*;

import java.io.*;

public class TCPServerEg {

    public static void main(String[] args) {

        try {

            ServerSocket serverSocket1 = new ServerSocket(12345);

            System.out.println("The TCP server is ON. It listening on port 12345");

             while (true) {

                Socket clientSocket1 = serverSocket1.accept();

                System.out.println("The TCP connection is from " + clientSocket1.getInetAddress());

                OutputStream outputStream = clientSocket1.getOutputStream();

                PrintWriter writer1 = new PrintWriter(outputStream, true);

                writer1.println("Thank you for choosing TCP Connection");

                clientSocket1.close();

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

Client:

              Client is the device or computer which gives the request to server.

Steps to implement the client program:

  • ·       Import the built-in packages.
  • ·       A code is written to develop the public class with main () function.
  • ·       Socket object is opened in an ip address with a port 12345.
  • ·       ‘getInputStream()’ -used to read the input as a stream.
  • ·       ‘BufferedReader’- Create the space for the input stream.
  • ·       ‘InputStream Reader’ - it gets the inputstream.
  • ·       ‘readLine()’ – it is the BufferedReader Object’s function to get the input.

Client Program:

import java.net.*;

import java.io.*;

public class TCPClientEg {

    public static void main(String[] args) {

        try {

            Socket socket1 = new Socket("127.0.0.1", 12345);

            InputStream inputStream1 = socket1.getInputStream();

            BufferedReader reader1 = new BufferedReader(new  

            InputStreamReader(inputStream1));

            System.out.println(reader1.readLine());

            socket1.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

Output:

Client Output:

C:\raji\blog>javac TCPClientEg.java

C:\raji\blog>java TCPClientEg

Thank you for choosing TCP Connection

C:\raji\blog>javac TCPServerEg.java

Server Output:

C:\raji\blog>java TCPServerEg

The TCP server is ON. It listening on port 12345

The TCP connection is from /127.0.0.1

This is the way of creating client server program using TCP is implemented successfully.

No comments:

Post a Comment