Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

buffered reader not receiving data from socket

I am writing a client application that will receive a continuous flow of data through tcp/ip. The problem I'm having is that the buffered reader object isn't receiving any data and is hanging at the readline method.

The way the server works is that you connect to it, and then send authentication information in order to receive data. The gist of my code is below

socket = new Socket(strHost, port);
authenticate();
inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
process(inStream);

authenticate()
{      
  PrintWriter pwriter = new PrintWriter(socket.getOutputStream(), true);
  pwriter.println(authString);
}

process(BufferedReader bufferedReader)
{
   while((line = bufferedReader.readLine()) != null)
      dostuff
}

I created a sample server application that sends data the way (I think) the server is sending data and it connects, and receives and processes the data fine. I can connect to the server fine in my application. I can also telnet to the server and write the authentication string and receive a flood of data using telnet. However my application just hangs at readLine with the server and I'm out of idea's why.

The data coming in (through telnet atleast) looks like a continuous stream of the following:

 data;data;data;data;data
 data;data;data;data;data

Why is my app hanging at readline, am I not outputting the authentication line correctly? I'm not receiving any errors...

EDIT My sample server code (which is working correctly)...again this is only mimicking the way I think the real server is running but I can connect to both in my application just not receive data from the real server.

  public static void main(String[] args) throws IOException
  {
  ServerSocket serverSocket = null;

  try
  {
     serverSocket = new ServerSocket(1987);
  }
  catch (IOException e)
  {
     System.out.println("Couldn't listen on port: 1987");
     System.exit(-1);
  }

  Socket clientSocket = null;
  try
  {
     clientSocket = serverSocket.accept();
  }
  catch (IOException e) {
     System.out.println("Accept failed: 1987");
     System.exit(-1);
  }

  PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  String something;

  while ((something = in.readLine()) != null)
  {
     while(true)
     {
        out.println(message);
     }
  }



  out.close();
  in.close();
  clientSocket.close();
  serverSocket.close();
}
like image 464
Ryan Avatar asked Jun 15 '11 15:06

Ryan


People also ask

How do I reset my buffer reader?

The reset() method of BufferedReader class in Java is used to fix or mark the position at the last marked position so that the same byte can be read again. Overrides: It overrides the reset() method of Reader class. Parameters: The method does not accept any parameter.

How do buffer readers take input?

BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); Once we have created a BufferedReader we can use its method readLine() to read one line of characters at a time from the keyboard and store it as a String object. String inputString = input. readLine();

What should I import for buffered reader?

Create a BufferedReader BuferedReader package first. Once we import the package, here is how we can create the reader. // Creates a FileReader FileReader file = new FileReader(String file); // Creates a BufferedReader BufferedReader buffer = new BufferedReader(file);

What is the difference between BufferedReader and BufferedInputStream?

The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes. The Java BufferedReader class is a subclass of the Java Reader class, so you can use a BufferedReader anywhere a Reader is required.


2 Answers

Firstly you should call BufferedReader.ready() before calling readLine(), as the ready() will tell you if it's ok to read.

PrintWriter doesn't throw I/O Exception so the write may have failed without your knowledge which is why there is nothing to read. Use PrintWriter.checkError() to see if anything as gone wrong during the write.

You ought to set up the input and output streams on the Socket at the same time before you write anything down the pipe. If your reader is not ready when the other end tries to write you will get a broken pipe in the server and it won't send any more data. Telnet sets up read and write before you have written or read anything.

You can make use of Wireshark to tell if the server is actually sending data.

like image 68
David Newcomb Avatar answered Oct 19 '22 07:10

David Newcomb


BufferdReader.readLine() reads lines, i.e. sequences of characters ended with \r or \r\n. I guess that your server writes its output into one single line. Your telnet output proves this assumption. Just use PrintWriter.println() at server side.

like image 10
AlexR Avatar answered Oct 19 '22 05:10

AlexR