Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to send to the socket EOF flag? Only socket.close() works fine :)

843790Mar 22 2009 — edited Mar 22 2009
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 19 2009
Added on Mar 22 2009
2 comments
2,475 views