Thursday, February 27, 2014
java socket receiving data problem
BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
System.out.println (reader.readLine ());
Why am I not read the above data, the following can be?
byte [] buf = new byte [1024];
int len = 0;
InputStream input = socket.getInputStream ();
while ((len = input.read (buf))! = -1) {
System.out.println (new String (buf, 0, len));
}
I see a lot of string buffer flow as much as possible on the Internet, you can sometimes get why the buffered data stream, but must use native String, seeking advice
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
System.out.println (reader.readLine ());
This is a must read by line newline '\ n'
InputStream input = socket.getInputStream ();
while ((len = input.read (buf))! = -1) {
System.out.println (new String (buf, 0, len));
}
This is the way byte streams, apparently able to read, read buffer loop continues until the passage of the stream is gone.
Reply:
public static byte [] readInputStream (InputStream ins) {
BufferedInputStream bis = new BufferedInputStream (ins);
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
try {
byte [] buffer = new byte [128];
int n = -1;
while ((n = bis.read (buffer))! = -1) {
bos.write (buffer, 0, n);
}
} Catch (IOException e) {
e.printStackTrace ();
return null;
} Finally {
if (bis! = null) {
try {
bis.close ();
} Catch (IOException e) {
e.printStackTrace ();
}
}
}
return bos.toByteArray ();
}
Can read the complete IO streams. Try not to use a character stream, line by line read, need each other to send \ n symbol to be read.
Reply:
Assuming the server has only one row of data returned, the client can not use the buffer stream data, which is why?
Reply:
Really odd strange, I used your code to receive the data stream, or receive eh
Reply:
Are you sure the server sends data yet?
This is not a major problem, just write a simple server can send data.
Reply:
Hair and I used
InputStream input = socket.getInputStream ();
while ((len = input.read (buf))! = -1) {
System.out.println (new String (buf, 0, len));
}
Receive data ah
Reply:
There are people to help me look at it
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment