copy bytes via sockets - bytes get lost
Hi Everyone,
I would be glad, if you could help me, get this piece of code running. It is a modification of the code, posted by "bbritta" in the following thread:
http://forum.java.sun.com/thread.jsp?forum=54&thread=446576
Description:
(two threads, which are running in an endless loop)
- the server waits for the command "next"
- the server responds with [number of bytes]
- the server sends [number of bytes] bytes
- the client post a message "next" to request some bytes
- the client reads [number of bytes], so he knows how many bytes are following
- the client reads [number of bytes] bytes
Problem/Question:
- the client gets stuck in the read-Method, because it didn't read all the bytes yet
- meanwhile the server is waiting for the "next" command, because he already sent all the bytes
What is my problem? How do I get this solved, without closing the socket?
publicclass Test{
privatestaticfinalint CHUNK_SIZE = 8192;
publicstaticvoid main(String[] arghs){
// Server-Thread
new Thread(new Runnable(){
publicvoid run(){
try{
ServerSocket ss =new ServerSocket(9998);
byte[] bytes =newbyte[1024];
for (int i = 0; i < bytes.length; i++) bytes[i] = (byte) i;
Socket s = ss.accept();
InputStream is = s.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(is));
OutputStream os = s.getOutputStream();
while (true){
reader.readLine();// reads the "next", so the other Thread wants another chunk
os.write((CHUNK_SIZE +"\r\n").getBytes());// how many bytes will be sent this time?
os.flush();
int bytesSent = 0;
while (bytesSent < CHUNK_SIZE){
os.write(bytes);
bytesSent += bytes.length;
}
System.out.println("out=" + bytesSent);
System.out.flush();// flushing System.out, so we see immediatly when Thread arrives here
os.flush();
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
// Client-Thread
new Thread(new Runnable(){
publicvoid run(){
try{
byte[] bytes =newbyte[1024];
Socket s =new Socket("127.0.0.1", 9998);
InputStream is = s.getInputStream();
BufferedReader reader =new BufferedReader(new InputStreamReader(is));
OutputStream os = s.getOutputStream();
while (true){
os.write("next\r\n".getBytes());// 'I want the next chunk'
os.flush();
String line = reader.readLine();// 'How large is it?'
int length = Integer.parseInt(line);
int x, bytesread = 0;
while (bytesread < length){
x = is.read(bytes);// after some cycles, this Thread gets stuck here . while the other one waits for the "next" command
bytesread += x;
System.out.println(" in=" + bytesread);
System.out.flush();// flushing System.out, so we see immediatly when Thread arrives here
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}).start();
}
}
examle output:
in=1024
in=2048
in=3072
in=4096
in=5120
in=6144
in=7168
in=8192
out=8192
in=1024
in=2048
in=3072
in=4096
in=5120
in=6144
in=7168
in=8192
out=8192
in=1024
in=2048
in=3072
in=4096
in=5120
in=6144
in=7168
in=8192
out=8192
in=1024
in=2048
in=3072
in=4096
in=5120
in=6144
in=7168
in=8192
out=8192
out=8192
in=1024
in=2048
in=3072
in=4096
in=5120
in=6144
in=7168
in=8192
out=8192
in=6
[the output ends here, while it should go one forever]
Lost Crumb

