[HELP] Simple Socket program, but the bufferedReader doesn't work

I have written a simple socket programming like this that used to connect the ftp server. I can get response from the server "220", but then the program is hanged.
Then, I used debugger to trace it and I found that the problem is due to the readLine() in the while loop.
But, I don't know what problem is and how to solve it?
Thanks

[code]import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;


public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Socket clientSocket = null;
DataOutputStream connectionToServer = null;
BufferedReader inFromConnection = null;
final String ip = "192.168.1.3";
final int port = 21;

try {
clientSocket = new Socket(ip, port);
connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
inFromConnection = new BufferedReader(new InputStreamReader(clientSocket
.getInputStream()));

String reply;

while ( (reply = inFromConnection.readLine())!= null) {
System.out.println("Response: " + reply);
}

System.out.println("TEST");

} catch (IOException e) {
System.out.println("ERROR");
}
}
}[/code]

Comments

  • : I have written a simple socket programming like this that used to
    : connect the ftp server. I can get response from the server "220",
    : but then the program is hanged.
    : Then, I used debugger to trace it and I found that the problem is
    : due to the readLine() in the while loop.
    : But, I don't know what problem is and how to solve it?
    : Thanks
    :
    : [code]: import java.io.BufferedReader;
    : import java.io.DataOutputStream;
    : import java.io.IOException;
    : import java.io.InputStreamReader;
    : import java.net.Socket;
    :
    :
    : public class test {
    : public static void main(String[] args) {
    : // TODO Auto-generated method stub
    : Socket clientSocket = null;
    : DataOutputStream connectionToServer = null;
    : BufferedReader inFromConnection = null;
    : final String ip = "192.168.1.3";
    : final int port = 21;
    :
    : try {
    : clientSocket = new Socket(ip, port);
    : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : inFromConnection = new BufferedReader(new InputStreamReader(clientSocket
    : .getInputStream()));
    :
    : String reply;
    :
    : while ( (reply = inFromConnection.readLine())!= null) {
    : System.out.println("Response: " + reply);
    : }
    :
    : System.out.println("TEST");
    :
    : } catch (IOException e) {
    : System.out.println("ERROR");
    : }
    : }
    : }[/code]:
    :
    readLine() assumes that the incoming stream is a text file, which has line endings. This might not be the case, which would block the readLine() indefinitely, because there's no line ending coming even though the incoming stream has ended.
    Secondly the Reader class assumes that the incoming stream is char-based. Because you cannot determine beforehand if the incoming stream is text based or binary, this might lead to corrupted data.
    The solution to both problems is quite simple: don't use the BufferedReader, but the BufferedInputStream. Also don't use a String to store the incoming data, but a ByteBuffer. This allows you to handle any type of stream from the server, and if the stream is text based convert it into a CharBuffer.
  • Thanks for your reply.
    But I don't know how to use those Class, can you give me some examples?
    because I cannot find any suitable example from the web.
    And I try to write those code but still doesn't work

    [code]import java.io.BufferedInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;


    public class test {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Socket clientSocket = null;
    DataOutputStream connectionToServer = null;
    BufferedInputStream inFromConnection = null;
    final String ip = "192.168.1.3";
    final int port = 12000;

    try {
    clientSocket = new Socket(ip, port);
    connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    inFromConnection = new BufferedInputStream(clientSocket.getInputStream());

    String reply;
    int i;
    ByteBuffer re = ByteBuffer.allocate(1024);
    while ( (i = inFromConnection.read())!= -1) {
    System.out.print((char)i);
    }

    System.out.println("TEST");

    } catch (IOException e) {
    System.out.println("ERROR");
    }
    }
    }[/code]

    : : I have written a simple socket programming like this that used to
    : : connect the ftp server. I can get response from the server "220",
    : : but then the program is hanged.
    : : Then, I used debugger to trace it and I found that the problem is
    : : due to the readLine() in the while loop.
    : : But, I don't know what problem is and how to solve it?
    : : Thanks
    : :
    : : [code]: : import java.io.BufferedReader;
    : : import java.io.DataOutputStream;
    : : import java.io.IOException;
    : : import java.io.InputStreamReader;
    : : import java.net.Socket;
    : :
    : :
    : : public class test {
    : : public static void main(String[] args) {
    : : // TODO Auto-generated method stub
    : : Socket clientSocket = null;
    : : DataOutputStream connectionToServer = null;
    : : BufferedReader inFromConnection = null;
    : : final String ip = "192.168.1.3";
    : : final int port = 21;
    : :
    : : try {
    : : clientSocket = new Socket(ip, port);
    : : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : : inFromConnection = new BufferedReader(new InputStreamReader(clientSocket
    : : .getInputStream()));
    : :
    : : String reply;
    : :
    : : while ( (reply = inFromConnection.readLine())!= null) {
    : : System.out.println("Response: " + reply);
    : : }
    : :
    : : System.out.println("TEST");
    : :
    : : } catch (IOException e) {
    : : System.out.println("ERROR");
    : : }
    : : }
    : : }[/code]: :
    : :
    : readLine() assumes that the incoming stream is a text file, which
    : has line endings. This might not be the case, which would block the
    : readLine() indefinitely, because there's no line ending coming even
    : though the incoming stream has ended.
    : Secondly the Reader class assumes that the incoming stream is
    : char-based. Because you cannot determine beforehand if the incoming
    : stream is text based or binary, this might lead to corrupted data.
    : The solution to both problems is quite simple: don't use the
    : BufferedReader, but the BufferedInputStream. Also don't use a String
    : to store the incoming data, but a ByteBuffer. This allows you to
    : handle any type of stream from the server, and if the stream is text
    : based convert it into a CharBuffer.


  • : Thanks for your reply.
    : But I don't know how to use those Class, can you give me some
    : examples?
    : because I cannot find any suitable example from the web.
    : And I try to write those code but still doesn't work
    :
    : [code]: import java.io.BufferedInputStream;
    : import java.io.DataOutputStream;
    : import java.io.IOException;
    : import java.net.Socket;
    : import java.nio.ByteBuffer;
    : import java.nio.channels.FileChannel;
    :
    :
    : public class test {
    : public static void main(String[] args) {
    : // TODO Auto-generated method stub
    : Socket clientSocket = null;
    : DataOutputStream connectionToServer = null;
    : BufferedInputStream inFromConnection = null;
    : final String ip = "192.168.1.3";
    : final int port = 12000;
    :
    : try {
    : clientSocket = new Socket(ip, port);
    : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : inFromConnection = new BufferedInputStream(clientSocket.getInputStream());
    :
    : String reply;
    : int i;
    : ByteBuffer re = ByteBuffer.allocate(1024);
    : while ( (i = inFromConnection.read())!= -1) {
    : System.out.print((char)i);
    : }
    :
    : System.out.println("TEST");
    :
    : } catch (IOException e) {
    : System.out.println("ERROR");
    : }
    : }
    : }[/code]:
    :
    : : : I have written a simple socket programming like this that used to
    : : : connect the ftp server. I can get response from the server "220",
    : : : but then the program is hanged.
    : : : Then, I used debugger to trace it and I found that the problem is
    : : : due to the readLine() in the while loop.
    : : : But, I don't know what problem is and how to solve it?
    : : : Thanks
    : : :
    : : : [code]: : : import java.io.BufferedReader;
    : : : import java.io.DataOutputStream;
    : : : import java.io.IOException;
    : : : import java.io.InputStreamReader;
    : : : import java.net.Socket;
    : : :
    : : :
    : : : public class test {
    : : : public static void main(String[] args) {
    : : : // TODO Auto-generated method stub
    : : : Socket clientSocket = null;
    : : : DataOutputStream connectionToServer = null;
    : : : BufferedReader inFromConnection = null;
    : : : final String ip = "192.168.1.3";
    : : : final int port = 21;
    : : :
    : : : try {
    : : : clientSocket = new Socket(ip, port);
    : : : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : : : inFromConnection = new BufferedReader(new InputStreamReader(clientSocket
    : : : .getInputStream()));
    : : :
    : : : String reply;
    : : :
    : : : while ( (reply = inFromConnection.readLine())!= null) {
    : : : System.out.println("Response: " + reply);
    : : : }
    : : :
    : : : System.out.println("TEST");
    : : :
    : : : } catch (IOException e) {
    : : : System.out.println("ERROR");
    : : : }
    : : : }
    : : : }[/code]: : :
    : : :
    : : readLine() assumes that the incoming stream is a text file, which
    : : has line endings. This might not be the case, which would block the
    : : readLine() indefinitely, because there's no line ending coming even
    : : though the incoming stream has ended.
    : : Secondly the Reader class assumes that the incoming stream is
    : : char-based. Because you cannot determine beforehand if the incoming
    : : stream is text based or binary, this might lead to corrupted data.
    : : The solution to both problems is quite simple: don't use the
    : : BufferedReader, but the BufferedInputStream. Also don't use a String
    : : to store the incoming data, but a ByteBuffer. This allows you to
    : : handle any type of stream from the server, and if the stream is text
    : : based convert it into a CharBuffer.
    :
    :
    :
    Here is a code snippet, which allocates a ByteBuffer called buffer of 2kB and loads the contents of a BufferedInputStream (dataInput) into it:
    [code]
    buffer = ByteBuffer.allocate(2048);
    while (dataInput.read(buffer.array(), 0, buffer.capacity()) == buffer.capacity) {
    // use Buffer to process the file, for example:
    for (int i = 0; i < buffer.capacity(); i++) {
    sum += buffer.get(i);
    }
    }
    // Read the last (less than 2kB) block
    int len = dataInput.read(buffer.array(), 0, buffer.capacity());
    if (len > -1) {
    for (int i = 0; i < buffer.capacity(); i++) {
    sum += buffer.get(i);
    }
    }
    [/code]
    As an example, I've included summing all the bytes.
    For more possibilities and methods of the ByteBuffer and InputStream, see the JavaDoc.
  • Actually, I tried similar code. But the read() command is non-stopped
    I checked the JavaDoc, when it reached the end of the stream, read will return -1. But it can't work.

    [code]import java.io.BufferedInputStream;
    import java.io.DataOutputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.net.Socket;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;


    public class test {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Socket clientSocket = null;
    DataOutputStream connectionToServer = null;
    BufferedInputStream inFromConnection = null;
    final String ip = "192.168.1.3";
    final int port = 12000;

    try {
    clientSocket = new Socket(ip, port);
    connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    inFromConnection = new BufferedInputStream(clientSocket.getInputStream());

    String reply;
    ByteBuffer buffer = ByteBuffer.allocate(2048);
    while (inFromConnection.read(buffer.array(), 0, buffer.capacity()) != -1) {
    // use Buffer to process the file, for example:
    for (int i = 0; i < buffer.capacity(); i++) {
    System.out.print((char)buffer.get(i));
    }
    }

    System.out.println("TEST");

    } catch (IOException e) {
    System.out.println("ERROR");
    }
    }
    }[/code]

    : : Thanks for your reply.
    : : But I don't know how to use those Class, can you give me some
    : : examples?
    : : because I cannot find any suitable example from the web.
    : : And I try to write those code but still doesn't work
    : :
    : : [code]: : import java.io.BufferedInputStream;
    : : import java.io.DataOutputStream;
    : : import java.io.IOException;
    : : import java.net.Socket;
    : : import java.nio.ByteBuffer;
    : : import java.nio.channels.FileChannel;
    : :
    : :
    : : public class test {
    : : public static void main(String[] args) {
    : : // TODO Auto-generated method stub
    : : Socket clientSocket = null;
    : : DataOutputStream connectionToServer = null;
    : : BufferedInputStream inFromConnection = null;
    : : final String ip = "192.168.1.3";
    : : final int port = 12000;
    : :
    : : try {
    : : clientSocket = new Socket(ip, port);
    : : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : : inFromConnection = new BufferedInputStream(clientSocket.getInputStream());
    : :
    : : String reply;
    : : int i;
    : : ByteBuffer re = ByteBuffer.allocate(1024);
    : : while ( (i = inFromConnection.read())!= -1) {
    : : System.out.print((char)i);
    : : }
    : :
    : : System.out.println("TEST");
    : :
    : : } catch (IOException e) {
    : : System.out.println("ERROR");
    : : }
    : : }
    : : }[/code]: :
    : :
    : : : : I have written a simple socket programming like this that used to
    : : : : connect the ftp server. I can get response from the server "220",
    : : : : but then the program is hanged.
    : : : : Then, I used debugger to trace it and I found that the problem is
    : : : : due to the readLine() in the while loop.
    : : : : But, I don't know what problem is and how to solve it?
    : : : : Thanks
    : : : :
    : : : : [code]: : : : import java.io.BufferedReader;
    : : : : import java.io.DataOutputStream;
    : : : : import java.io.IOException;
    : : : : import java.io.InputStreamReader;
    : : : : import java.net.Socket;
    : : : :
    : : : :
    : : : : public class test {
    : : : : public static void main(String[] args) {
    : : : : // TODO Auto-generated method stub
    : : : : Socket clientSocket = null;
    : : : : DataOutputStream connectionToServer = null;
    : : : : BufferedReader inFromConnection = null;
    : : : : final String ip = "192.168.1.3";
    : : : : final int port = 21;
    : : : :
    : : : : try {
    : : : : clientSocket = new Socket(ip, port);
    : : : : connectionToServer = new DataOutputStream(clientSocket.getOutputStream());
    : : : : inFromConnection = new BufferedReader(new InputStreamReader(clientSocket
    : : : : .getInputStream()));
    : : : :
    : : : : String reply;
    : : : :
    : : : : while ( (reply = inFromConnection.readLine())!= null) {
    : : : : System.out.println("Response: " + reply);
    : : : : }
    : : : :
    : : : : System.out.println("TEST");
    : : : :
    : : : : } catch (IOException e) {
    : : : : System.out.println("ERROR");
    : : : : }
    : : : : }
    : : : : }[/code]: : : :
    : : : :
    : : : readLine() assumes that the incoming stream is a text file, which
    : : : has line endings. This might not be the case, which would block the
    : : : readLine() indefinitely, because there's no line ending coming even
    : : : though the incoming stream has ended.
    : : : Secondly the Reader class assumes that the incoming stream is
    : : : char-based. Because you cannot determine beforehand if the incoming
    : : : stream is text based or binary, this might lead to corrupted data.
    : : : The solution to both problems is quite simple: don't use the
    : : : BufferedReader, but the BufferedInputStream. Also don't use a String
    : : : to store the incoming data, but a ByteBuffer. This allows you to
    : : : handle any type of stream from the server, and if the stream is text
    : : : based convert it into a CharBuffer.
    : :
    : :
    : :
    : Here is a code snippet, which allocates a ByteBuffer called buffer
    : of 2kB and loads the contents of a BufferedInputStream (dataInput)
    : into it:
    : [code]:
    : buffer = ByteBuffer.allocate(2048);
    : while (dataInput.read(buffer.array(), 0, buffer.capacity()) == buffer.capacity) {
    : // use Buffer to process the file, for example:
    : for (int i = 0; i < buffer.capacity(); i++) {
    : sum += buffer.get(i);
    : }
    : }
    : // Read the last (less than 2kB) block
    : int len = dataInput.read(buffer.array(), 0, buffer.capacity());
    : if (len > -1) {
    : for (int i = 0; i < buffer.capacity(); i++) {
    : sum += buffer.get(i);
    : }
    : }
    : [/code]:
    : As an example, I've included summing all the bytes.
    : For more possibilities and methods of the ByteBuffer and
    : InputStream, see the JavaDoc.


  • : Actually, I tried similar code. But the read() command is non-stopped
    : I checked the JavaDoc, when it reached the end of the stream, read
    : will return -1. But it can't work.
    :
    As taken directly from the JavaDoc of the BufferedInputStream.read(array, int, int):
    [code]
    This iterated read continues until one of the following conditions becomes true:

    * The specified number of bytes have been read,
    * The read method of the underlying stream returns -1, indicating end-of-file, or
    * The available method of the underlying stream returns zero, indicating that further input requests would block.
    [/code]
    When your are reading a file larger than 2 kB, the read() call in the while-loop will return after reading 2 kB, because the first condition is met (2 kB is read). This will allow the code within the loop to process that block of the file.
    True, my code contains a few errors, because I didn't read the JavaDoc carefully enough (using a different BufferedInputStream). The code needs some refinement when it reaches the final block of the file, but the general idea works: it reads the file in blocks of bytes and processes those bytes within the body of the loop.
  • Thanks, I am still trying because I sometimes find some bugs.

    : : Actually, I tried similar code. But the read() command is non-stopped
    : : I checked the JavaDoc, when it reached the end of the stream, read
    : : will return -1. But it can't work.
    : :
    : As taken directly from the JavaDoc of the
    : BufferedInputStream.read(array, int, int):
    : [code]:
    : This iterated read continues until one of the following conditions becomes true:
    :
    : * The specified number of bytes have been read,
    : * The read method of the underlying stream returns -1, indicating end-of-file, or
    : * The available method of the underlying stream returns zero, indicating that further input requests would block.
    : [/code]:
    : When your are reading a file larger than 2 kB, the read() call in
    : the while-loop will return after reading 2 kB, because the first
    : condition is met (2 kB is read). This will allow the code within the
    : loop to process that block of the file.
    : True, my code contains a few errors, because I didn't read the
    : JavaDoc carefully enough (using a different BufferedInputStream).
    : The code needs some refinement when it reaches the final block of
    : the file, but the general idea works: it reads the file in blocks of
    : bytes and processes those bytes within the body of the loop.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories