Posted By:
21 Feb, 2014 9:40 am PST

Programs to establish communication between Server and the Client

Write a program to create a server such that the server receives data from the client using BufferedReader and then sends reply to the client using PrintStream.

//to create a server that receives data from client and sends response to client

import java.net.*;

import java.io.*;

public class ClientConnectServer{

public static void main(String [] args) throws Exception

{

       //create server socket

       ServerSocket s=new ServerSocket(999);

       //establish connection with client socket

       Socket e=s.accept();

       System.out.println("Connection established");

       //to transfer data to client

       PrintStream p=new PrintStream(e.getOutputStream());

       //to receive data from client

BufferedReader m=new BufferedReader(new InputStreamReader(e.getInputStream()));

       //to read data from keyboard

       BufferedReader v=new BufferedReader(new InputStreamReader(System.in));

       while(true)

       {

              String str1,str2;

              while((str1=m.readLine())!=null) // read data from client

              {

              System.out.println(str1);

              str2=v.readLine();

              p.println(str2);

              }

       //close all connections

              m.close();

              p.close();

              v.close();

              s.close();

              e.close();

              System.exit(0);

                     }//end of while loop

       }

}

Develop a code to create a client which first connects to a server, then starts the communication by sending a string to the server. The server then responds to the client. When ‘exit’ is typed at client side, the program terminates.

import java.net.*;

import java.io.*;

public class ClientConnectServer2 {

        public static void main(String[] args) throws Exception{

              //Create a client socket

              Socket t=new Socket("localhost",999);

              //send data to server

              DataOutputStream d=new DataOutputStream(  t.getOutputStream());

              //to read data sent from server

              BufferedReader g=new BufferedReader(new InputStreamReader(t.getInputStream()));

                     //to read input from keyboard

              BufferedReader b=new BufferedReader(new InputStreamReader(System.in));

              String q,w;

              //repeat untill'exit' is typed at client side

              while(!(q=b.readLine()).equals("exit")){

                     d.writeBytes(q +"\n"); //send data to server

                     w=g.readLine();//receive data from server

              System.out.println(w);

              }

              //close connections

              b.close();g.close();

              d.close();t.close();

       }

 }

OUTPUT:

 



Description:

Java.net : Provides the classes for implementing networking applications.

Serversocket class: This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester.

Socket : This class implements client sockets .

 

Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud