Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send "Hello" from Server to Client on getting connected... Server side program is running properly but Client side code is having a problem that "Data is not ready to read"

These are my codes... Please Help...

Server Side :
Java

import java.net.*;
import java.io.*;

public class ServerSide
{
  	public static void main(String args[])
	{	
	       try
	       {	
        	   ServerSocket ss = new ServerSocket(8888);
		   System.out.println("Waiting...");	
        	   Socket server=ss.accept();
	           PrintStream ps= new PrintStream(server.getOutputStream());
        	   ps.print("Hello...");
	           ps.flush();
		   System.out.println("Data Sent...");
			
	       }
	       catch(Exception e)
	       {
			 System.out.println("Error : " + e.toString());
	       }
	   }
}



Client Side :

Java
import java.net.*;
import java.io.*;

public class ClientSide 
{
	public static void main(String args[])
	{
		try
	       {
		   String str= new String();
	           Socket client=new Socket(InetAddress.getLocalHost(),8888);
        	   BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
                   if(br.ready())
                   {
                        str=br.readLine();
        	        System.out.println(str);
                   }
	           else
                   {
                        System.out.println("Data not ready to read from Stream");
                   }
	       }
	       catch(Exception e)
	       {
			System.out.println("Error : " + e.toString());
	       }
	}
}
Posted
Updated 14-Aug-13 4:14am
v2

1 solution

This just means that the client is trying to read from the stream before the server has completed its write function. In normal operation your client code should wait for some short time and then re-poll the stream to see if any data has appeared.
 
Share this answer
 
Comments
Sarjit 14-Aug-13 11:53am    
Thank you Richard... I understood what was the problem... I changed my code but now Connection Reset error is coming... Please help by seeing this code... Thank you

try
{
String str= new String();
Socket client=new Socket(InetAddress.getLocalHost(),8888);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));

while(br.ready()==false)
{
System.out.println("Still Not ready");
}
if(br.ready())
{
str=br.readLine();
System.out.println(str);
}
else
{
System.out.println("Not ready...");
}

}
catch(Exception e)
{
System.out.println("Error : " + e.toString());
}
Richard MacCutchan 14-Aug-13 12:04pm    
Well, it's broadly the same issue. your server starts up, sends a message and then terminates. At which time the socket is closed and the client gets a "reset" error. The server should keep running until all clients close their connections. Have a look at this tutorial for some useful detail and sample code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900