I send a binary file from server socket to client socket. And I have some problem with client (but actually with server :) ).
That is my server code which sends a file:
OutputStream os = null;
FileInputStream fis = null;
try{
fis = new FileInputStream( "some-file.png" );
} catch ( FileNotFoundException e1 ) {}
try {
os = socket.getOutputStream();
int c = -1;
while( ( c = fis.read() ) != -1 ) {
os.write( c );
}
os.flush();
// os.close();
} catch ( IOException e2 ) {}
And that is client which receives a file:
try {
int sChunk = 8192;
InputStream is = socket.getInputStream();
FileOutputStream out = new FileOutputStream( "file.png" );
byte[] buffer = new byte[ sChunk ];
int length;
while ( ( length = is.read( buffer, 0, sChunk ) ) > -1 )
out.write( buffer, 0, length );
out.close();
} catch ( IOException e ) {
}
A client's while never continues cause I don't know how to send EOF from server.
If I do: "os.close();" at the end of server sending - it works... But.. It closes my socket! But I want to work with socket after that :)
How to send correct EOF flag from server? I think it's the root of all problems here..
Edited by: JavaProger on Mar 22, 2009 4:43 PM