Java Networking Examples: Simple Client Server application

               A set of computers and other communication devices connected together to share data, communication and services. Clients and server are two important keywords in networking.

Client -It is the one which requests the service.

Server – It responds the clients according to its request.

Let us create a simple client and server communication using sockets.

Program implementation:

Client implementation:

              The client program implementation starts from including the built-in packages. Other steps are as follows.

·       Create a class with main() function.

·       An object is created for socket in the localhost in port 12345.

·       For input object creation, use BufferedReader class.

·       PrintWriter is used to develop output object.

·       A message is created and print it using output object.

·       Get the response from the server and print the output.

Program:

import java.io.*;

import java.net.*;

public class SimpleClientEg {

    public static void main(String[] args) {

        try (Socket socket = new Socket("localhost", 12345)) {

            PrintWriter oput = new PrintWriter(socket.getOutputStream(), true);

            BufferedReader iput = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String message = "Welcome to Java Networking";

            oput.println(message);

            String response = iput.readLine();

            System.out.println("The message Received from server is: " + response);

        } catch (IOException e) {

            System.err.println("The Client exception is: " + e.getMessage());

        }

    }

}

Server implementation:

The implementation of server has below steps.

·       Create a class with main() function.

·       An object is created for Serversocket in the port 12345.

·       Print a message that Server is listening on port 12345.

·       A socket object is created and accept the ServerSocket.

·       PrintWriter is used to develop output object.

·       BufferedReader object is used to create a input object.

·       Get the message from client.

·       Provide the response from the server and print the output.

Server Program:

import java.io.*;

import java.net.*;

public class SimpleServerEg {

    public static void main(String[] args) {

        try (ServerSocket serverSocket = new ServerSocket(12345)) {

            System.out.println("Server is listening on port 12345");

            while (true) {

                try (Socket socket = serverSocket.accept()) {

                    System.out.println("Client is connected");

                    BufferedReader iput = new BufferedReader(new                                

                    InputStreamReader(socket.getInputStream()));

                    PrintWriter otput = new PrintWriter(socket.getOutputStream(), true);

                    String message = iput.readLine();

                    System.out.println("Message received from client: " + message);

                    String response = "The Server response is: " + message;

                    otput.println(response);

                }

            }

        } catch (IOException e) {

            System.err.println("Server exception: " + e.getMessage());

        }

    }

}

Output:

Server

C:\raji\blog>javac SimpleServerEg.java

C:\raji\blog>java SimpleServerEg

Server is listening on port 12345

Client is connected

Message received from client: Welcome to Java Networking

The client side output is given below.

Client:

C:\raji\blog>javac SimpleClientEg.java

C:\raji\blog>java SimpleClientEg

The message Received from server is: The Server response is: Welcome to Java Networking

That’s all. The simple way of creating Client server application is implemented. Keep coding..

No comments:

Post a Comment