Java Program to Send a Message from Client to Server and Receive a Response Back Using Socket Programming

What this program does?

This Java Program

1. Make Use of Java Socket Programming

2. It starts a server which will be always running listening to a port 25000 (Server.java)

3. Client (Client.java) sends a number (message) to the server

4. Server receives this number and multiplies it by 2

5. Server (Server.java) sends back the result (message) to the client (Client.java)

6. In case the number sent by the client was not a proper number, server (Server.java) sends back the message “Please send a proper number” to the client (Client.java)

Note: Please run Server.java because server should be ready before client sends the message to it.

Server.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server 
{

	private static Socket socket;

	public static void main(String[] args) 
	{
		try
		{

			int port = 25000;
			ServerSocket serverSocket = new ServerSocket(port);
			System.out.println("Server Started and listening to the port 25000");

			//Server is running always. This is done using this while(true) loop
			while(true) 
			{
				//Reading the message from the client
				socket = serverSocket.accept();
				InputStream is = socket.getInputStream();
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				String number = br.readLine();
				System.out.println("Message received from client is "+number);

				//Multiplying the number by 2 and forming the return message
				String returnMessage;
				try
				{
					int numberInIntFormat = Integer.parseInt(number);
					int returnValue = numberInIntFormat*2;
					returnMessage = String.valueOf(returnValue) + "\n";
				}
				catch(NumberFormatException e)
				{
					//Input was not a number. Sending proper message back to client.
					returnMessage = "Please send a proper number\n";
				}

				//Sending the response back to the client.
				OutputStream os = socket.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(os);
				BufferedWriter bw = new BufferedWriter(osw);
				bw.write(returnMessage);
				System.out.println("Message sent to the client is "+returnMessage);
				bw.flush();
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally 
		{
			try
			{
				socket.close();
			}
			catch(Exception e){}
		}
	}
}

 

Client.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class Client 
{

	private static Socket socket;

	public static void main(String args[])
	{
		try 
		{
			String host = "localhost";
			int port = 25000;
			InetAddress address = InetAddress.getByName(host);
			socket = new Socket(address, port);

			//Send the message to the server
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			BufferedWriter bw = new BufferedWriter(osw);

			String number = "2";

			String sendMessage = number + "\n";
			bw.write(sendMessage);
			bw.flush();
			System.out.println("Message sent to the server : "+sendMessage);

			//Get the return message from the server
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String message = br.readLine();
			System.out.println("Message received from the server : " +message);
		}
		catch (Exception exception) 
		{
			exception.printStackTrace();
		}
		finally
		{
			//Closing the socket
			try
			{
				socket.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}

 


Comments(59)


Krishna 07 Oct 2014 8:58 PM
Thanks Working Fine.....
Like (0)| Dislike (0)| Reply| Flag

anto 28 Sep 2014 6:27 PM
server response not showing swing text filed window stuck 
Like (0)| Dislike (0)| Reply| Flag

anvita rathi 12 Aug 2014 3:31 PM
how to connect client and a server in a way that the server gets information about the usage of data for the client?
Like (0)| Dislike (0)| Reply| Flag

manish 05 Aug 2014 10:32 PM
anisha make sure use only net beans not notepad or any other things
Like (0)| Dislike (0)| Reply| Flag

manish 05 Aug 2014 10:31 PM
tx....
Like (0)| Dislike (0)| Reply| Flag

saroja 25 Jul 2014 3:21 PM
can we execute this by using only while loop,except try catch?
Like (0)| Dislike (0)| Reply| Flag

Brock 23 Jul 2014 8:39 PM
Thanks Working Fine For Me.....God Bless u :)
Like (0)| Dislike (0)| Reply| Flag

Chandan Singh 21 Jul 2014 12:22 AM
HI,
     I am not getting any response from server when  i am trying to give input from console.

Like (0)| Dislike (0)| Reply| Flag

prashant 07 Jul 2014 4:16 PM
thnax...........
Like (0)| Dislike (0)| Reply| Flag

rupesh 01 Jun 2014 11:36 PM
plz tell me how client listen continues  server response.......
Like (0)| Dislike (0)| Reply| Flag

next last
Add a new comment ...


Name   Email

Please answer the simple math question given below

8 + 0 =


close

Sign in