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.
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");
}
}
}
: : 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
: :
: :
: : 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");
: : }
: : }
: : }: :
: :
: : : : 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
: : : :
: : : :
: : : : 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");
: : : : }
: : : : }
: : : : }: : : :
: : : :
: : : 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:
:
:
: 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);
: }
: }
: :
: As an example, I've included summing all the bytes.
: For more possibilities and methods of the ByteBuffer and
: InputStream, see the JavaDoc.