Monday, 22 December 2014

Client Server application in Java

Structure of the Problem Requirements 

In this problem we will learn how to implement a client server application in Java. A client /server application is a piece of software that runs on client system and make request to a remote server. First Run the server main class to active the server. After running the server start listening the request from client side. Now run the the client main class that send a message to server to connect and start chat. Here is the complete source code of this program which help you in better understanding.

Source Code 


Server.Java


package TCPserverApplication;

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.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class server {
public void simple_server() throws Exception{
Scanner in = new Scanner(System.in);
try{
ServerSocket server = new ServerSocket( 11111 , 1 , InetAddress.getByName("localhost"));//server will listen on port 11111
String message_for_client="go";
String sendMessage;
while(true)//The server will continue to listen for another client.
{
System.out.println("Waiting for request . . .");
Socket client1 = server.accept();
System.out.println("Accepted go on client cosole");
while(!(message_for_client=="exit"))
{
if(!client1.isClosed()){
InputStream input_stream = client1.getInputStream();
       InputStreamReader input_stream_reader = new InputStreamReader(input_stream );
       BufferedReader buffer_reader = new BufferedReader(input_stream_reader);
       String message = buffer_reader.readLine();
       
       if(message==null){
        System.out.println("Exited");//Server receives the message and displays on the screen
        client1.close();
       }else{
        System.out.println("From Client : "+message);//Server receives the message and displays on the screen
       
       OutputStream output_stream = client1.getOutputStream();
       OutputStreamWriter output_stream_writer= new OutputStreamWriter(output_stream);
       BufferedWriter buffered_writer = new BufferedWriter(output_stream_writer);
       
       System.out.println("Enter a message for Client:");
       
       message_for_client = in.nextLine();//server takes input from the keyboard and sends this to the client.
       sendMessage = message_for_client + "\n";
      
       buffered_writer.write(sendMessage);
       buffered_writer.flush();
       }
   }else{
    client1.close();
    break;
   }
}
}
}
catch (Exception e){
e.printStackTrace();
}
in.close();
}
}


Server_main.Java


package TCPserverApplication;

/*
 * CMS : 7667
 * Name : Uzair Ahmed
 * Topic : TCP Client and TCP Server application
 */
public class server_main {
public static void main(String[] args) throws Exception {
System.out.println("Welcome Server");
server s=new server();
s.simple_server();
}
}


Client.Java


package TCPclientApplications;
//SimpleClient.java: A simple client program.
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class client {
public void simple_client() throws Exception{
Socket client_socket = new Socket(InetAddress.getByName("localhost"),11111);
Scanner in = new Scanner(System.in);
String message_from_server;
String message_for_server="go";
String sendMessage;
        while(!(message_for_server=="exit"))
        {
    OutputStream output_stream = client_socket.getOutputStream();
       OutputStreamWriter output_stream_writer= new OutputStreamWriter(output_stream);
       BufferedWriter buffered_writer = new BufferedWriter(output_stream_writer);
       
       System.out.println("Enter a message for Server:");
       message_for_server = in.nextLine();// the client takes input from the keyboard and sends this message to server.
       if(message_for_server.equals("exit")){//On EXIT, the client closes its connection and terminates.
        System.out.println("EXITING");
        client_socket.close();
        System.exit(0);
       
       }
       sendMessage = message_for_server + "\n";
       
       buffered_writer.write(sendMessage);
       buffered_writer.flush();
       
       InputStream input_stream = client_socket.getInputStream();
       InputStreamReader input_stream_reader = new InputStreamReader(input_stream);
       BufferedReader buffered_reader = new BufferedReader(input_stream_reader );
       
       message_from_server = buffered_reader.readLine();
       System.out.println("Server : " +message_from_server);//Client receives the message and displays on the screen
        }
        
in.close();
client_socket.close();
}
}


Client_main.Java


package TCPclientApplications;

/*
 * CMS : 7667
 * Name : Uzair Ahmed
 * Topic : TCP Client and TCP Server application
 */
public class client_main {
public static void main(String[] args) throws Exception {
System.out.println("Welcome Client");
client c=new client();
c.simple_client();
}
}


Output of the Program

Client /Server Application in Java
Client /Server Application in Java

Client /Server Application in Java

Client /Server Application in Java

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment