This discussion is archived
9 Replies Latest reply: Jul 16, 2012 9:40 AM by EJP RSS

Unable to Implement Simple Echo Server & Client

Javaprog Newbie
Currently Being Moderated
When i'm running my Server 1st & running Client later , Client is getting connected but the message from client is not getting passed to Server

Here is the code for client & Server.

Code for Client:
     import java.io.*;
     import java.net.*;
     public class EchoClient {
     public static void main(String[] args) throws IOException 
     {
          Socket echoClient=null;
          BufferedReader br= null;
          PrintWriter pwr;
          try 
          {
               
               echoClient= new Socket("localhost",9999);
               pwr = new PrintWriter(echoClient.getOutputStream());
               br= new BufferedReader(new InputStreamReader(echoClient.getInputStream()));
               pwr.println("This is Client");
               System.out.println("Message received from server is ");
               while (br.ready())
               {
                    System.out.println(br.readLine());
               }
               
          
          }
          catch(UnknownHostException uhe)
          {
               uhe.printStackTrace();
          }
          //Write Data into Socket
          echoClient.close();
               
          
     }
     
     
     
}
          Code for Server:
     import java.io.*;
     import java.net.*;
     public class EchoServer {
  
     
     public static void main(String[] args) throws IOException
     {
          ServerSocket srvSock;
          Socket client;
          BufferedReader bfr;
          PrintWriter pw;
          
          try
          {
               srvSock= new ServerSocket(9999);
               System.out.println("Server Started & Listening for Client request\n");
               client= srvSock.accept();
               System.out.println("Client Connected!\n");
               bfr =new BufferedReader (new InputStreamReader(client.getInputStream()));
               System.out.println("Message from client is : "+bfr.readLine());
               pw= new PrintWriter(client.getOutputStream());
               pw.println("Message received in Server");
          }
          catch (SocketException se)
          {
               se.printStackTrace();
               
          }
          
          
          
          }
     }
I'm unable to fish out what exactly is happening.Plz Explain me the problem in detail.

Edited by: sabre150 on Jul 16, 2012 2:09 PM

Moderator action : { code} tags added. Please add them yourself in the future.
  • 1. Re: Unable to Implement Simple Echo Server & Client
    sabre150 Expert
    Currently Being Moderated
    1) You have not followed EJP's advice given in response to your previous thread i.e. you need to use flush().
    2) Why are you using
    while (br.ready())
                   {
                        System.out.println(br.readLine());
                   }
    Since it will just block until there is a line to read why not just read and print the line without the check for ready().
  • 2. Re: Unable to Implement Simple Echo Server & Client
    Javaprog Newbie
    Currently Being Moderated
    I've Altered the Code now. I've followed What EJP Said me and so Flushed the
    PrintWriter
    before reading in Client. Also I've removed
    while(br.ready())
    as you've mentioned me.

    Now the Present code looks like this:
    Code of client:
            import java.io.*;
    import java.net.*;
    public class EchoClient {
         public static void main(String[] args) throws IOException 
         {
              Socket echoClient=null;
              BufferedReader br= null;
              PrintWriter pwr;
              try 
              {
                   
                   echoClient= new Socket("localhost",9999);
                   pwr = new PrintWriter(echoClient.getOutputStream());
                   br= new BufferedReader(new InputStreamReader(echoClient.getInputStream()));
                   pwr.println("This is Client");
                   pwr.flush();
                   System.out.println("Message received from server is ");
                   System.out.println(br.readLine());
              }
              catch(UnknownHostException uhe)
              {
                   uhe.printStackTrace();
              }
              echoClient.close();
         }
    }
    Code of Server:
    import java.io.*;
    import java.net.*;
    public class EchoServer {
    
         
         public static void main(String[] args) throws IOException
         {
              ServerSocket srvSock;
              Socket client;
              BufferedReader bfr;
              PrintWriter pw;
              
              try
              {
                   srvSock= new ServerSocket(9999);
                   System.out.println("Server Started & Listening for Client request\n");
                   client= srvSock.accept();
                   System.out.println("Client Connected!\n");
                   bfr =new BufferedReader (new InputStreamReader(client.getInputStream()));
                   System.out.println("Message from client is : "+bfr.readLine());
                   pw= new PrintWriter(client.getOutputStream());
                   pw.println("Message received in Server");
              }
              catch (SocketException se)
              {
                   se.printStackTrace();
                   
              }
              
         }
    }
    Now When I'm executing this Program ....I've Run the Server 1st as usually and when I've Started Client it terminated with the following Exception code:
    Message received from server is 
    Exception in thread "main" java.net.SocketException: Connection reset
         at java.net.SocketInputStream.read(Unknown Source)
         at java.net.SocketInputStream.read(Unknown Source)
         at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
         at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
         at sun.nio.cs.StreamDecoder.read(Unknown Source)
         at java.io.InputStreamReader.read(Unknown Source)
         at java.io.BufferedReader.fill(Unknown Source)
         at java.io.BufferedReader.readLine(Unknown Source)
         at java.io.BufferedReader.readLine(Unknown Source)
         at EchoClient.main(EchoClient.java:18)
  • 3. Re: Unable to Implement Simple Echo Server & Client
    sabre150 Expert
    Currently Being Moderated
    Javaprog wrote:
    Now When I'm executing this Program ....I've Run the Server 1st as usually and when I've Started Client it terminated with the following Exception code:
    Message received from server is 
    Exception in thread "main" java.net.SocketException: Connection reset
         at java.net.SocketInputStream.read(Unknown Source)
         at java.net.SocketInputStream.read(Unknown Source)
         at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
         at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
         at sun.nio.cs.StreamDecoder.read(Unknown Source)
         at java.io.InputStreamReader.read(Unknown Source)
         at java.io.BufferedReader.fill(Unknown Source)
         at java.io.BufferedReader.readLine(Unknown Source)
         at java.io.BufferedReader.readLine(Unknown Source)
         at EchoClient.main(EchoClient.java:18)
    I don't get that exception but this may be a race condition since your server may exit and close the socket before anything can be sent. . Why are you still not flush() ing the PrintWriter in the server?
  • 4. Re: Unable to Implement Simple Echo Server & Client
    Javaprog Newbie
    Currently Being Moderated
    In the Server I've now Flushed the Print Writer as here:
    import java.io.*;
    import java.net.*;
    
    public class EchoServer {
    
         
         public static void main(String[] args) throws IOException,InterruptedException
         {
              ServerSocket srvSock;
              Socket client;
              BufferedReader bfr;
              PrintWriter pw;
              
              try
              {
                   srvSock= new ServerSocket(9999);
                   System.out.println("Server Started & Listening for Client request\n");
                   client= srvSock.accept();
                   System.out.println("Client Connected!\n");
                   bfr =new BufferedReader (new InputStreamReader(client.getInputStream()));
                   System.out.println("Message from client is : "+bfr.readLine());
                   pw= new PrintWriter(client.getOutputStream());
                   pw.println("Message received in Server");
                   pw.flush();
                   
              }
              catch (SocketException se)
              {
                   se.printStackTrace();
                   
              }
              
         }
    }
    Now I'm receiving No exception. But the O/p is as follows:

    In Client:
    Message received from server is

    And in Server:
    Message received in Server

    But no other Output is being Displayed!

    Edited by: Javaprog on Jul 16, 2012 7:44 PM
  • 5. Re: Unable to Implement Simple Echo Server & Client
    sabre150 Expert
    Currently Being Moderated
    You seem to have created a deadlock by in the client creating the BufferedReader before you send the message to the server. If using your client code I first send the data before creating the BufferedReader it works correctly or me.

    You might have to wait for EJP for an authoritative analysis.
  • 6. Re: Unable to Implement Simple Echo Server & Client
    Javaprog Newbie
    Currently Being Moderated
    Even though I've created BufferedReader after writing on socket. the same situation ...ending up with some or other errors :P
  • 7. Re: Unable to Implement Simple Echo Server & Client
    sabre150 Expert
    Currently Being Moderated
    Javaprog wrote:
    ...ending up with some or other errors :P
    which of course are of no interest to those trying to help you.
  • 8. Re: Unable to Implement Simple Echo Server & Client
    gimbal2 Guru
    Currently Being Moderated
    sabre150 wrote:
    Javaprog wrote:
    ...ending up with some or other errors :P
    which of course are of no interest to those trying to help you.
    </sarcasm>

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points