Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Develop a Java Application using which TCPServer will send a text message and TCPClient will receive it

Develop a Simple Java Application in using which TCPServer will send a text message and TCPClient will receive it 

In this program we have used the client_socket and the server_socket to initialising the connection between Client and Server, then the Server will send the message using DataOutputStream and Client will receive it using DataInputStream and readUTF().


Java Program - 

Client.java - 

package com.gpm.clientjava_2_2;
import java.io.*;
import java.net.*;

public class Client {
    // write your code here
        private Socket client_socket = null;
        private DataOutputStream output_stream = null;
        private DataInputStream input_stream = null;


        public Client(String host_address,int port_number) throws UnknownHostException,IOException
        {
            try
            {
                //socket initialization
                client_socket = new Socket(host_address,port_number);

                System.out.println("Connected to Server");
                output_stream = new DataOutputStream(client_socket.getOutputStream());

                //client to server communication
                System.out.println("Sending message to server >>");
                output_stream.writeUTF("Hello Server ..!!!");

                //Server to client communication
                System.out.println("Accepting message from server >>");
                input_stream = new DataInputStream(client_socket.getInputStream());

                String message = input_stream.readUTF().toString();
                System.out.println(message);

                //Closing connections
                output_stream.close();
                client_socket.close();
                input_stream.close();
            }
            catch(UnknownHostException e)
            { System.out.println(e); }
            catch(IOException e)
            { System.out.println(e); }
        }

        public static void main(String args[])
        {
            try
            {
                //local host address
                String host_address = "127.0.1.1";
                Client client = new Client(host_address,6060);
            }
            catch(UnknownHostException e)
            { System.out.println(e); }
            catch(IOException e)
            { System.out.println(e); }
        }
    }


Server.java - 

package com.gpm.clientjava_2_2;
import java.io.*;
import java.net.*;

public class Server {

    private ServerSocket server_socket = null;
    private Socket client_socket =null;
    private DataInputStream input_stream = null;
    private DataOutputStream output_stream = null;

    public Server(int port_number)
    {
        try
        {
            //Initialization of server side socket
            server_socket = new ServerSocket(port_number);
            System.out.println("Server started >>");

            System.out.println("Waiting for a client >>");
            client_socket = server_socket.accept();
            System.out.println("Client accepted");

            //Receiving message from client socket
            input_stream =new DataInputStream(new BufferedInputStream(client_socket.getInputStream()));

            String message = input_stream.readUTF();

            System.out.println(message);

            //Sending reply to client
            output_stream = new DataOutputStream(client_socket.getOutputStream());
            System.out.println("Sending reply to client >>");
            output_stream.writeUTF("Hello Client...!!");

            //Closing connections
            client_socket.close();
            input_stream.close();
            output_stream.close();
        }
        catch(IOException e)
        { System.out.println(e); }
    }

    public static void main(String args[])
    {
        Server server = new Server(6060);
    }
}


Output -

First of all, run the Server.java and then run the Client.java. 


Post a Comment

0 Comments

Ad Code

Responsive Advertisement