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

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

os = socket.getOutputStream();
int c = -1;
while( ( c = fis.read() ) != -1 ) {
os.write( c );
}That's about as inefficient as it could be. Try this:
int count;
byte[] buffer = new byte[8192];
while ((count = fis.read(buffer)) > 0)
os.write(buffer, 0, count);
os.close();
Hmm, you're doing that in the client, why not in the server?
} catch ( IOException e2 ) {}Never ignore an exception. In this case you should close the socket.
} catch ( IOException e ) {See above.
A client's while never continues cause I don't know how to send EOF from server.You can't send an EOF from the server. You can cause an EOS (end of stream on the socket) at the reader, by closing the socket, or shutting down its output, but then you can't send anything else on the socket. You have two choices:
(a) use a connection per file. If the files are large the overhead is minimal.
(b) use an application protocol to tell the reader when the file is complete. Typically this takes the form of sending the file length as a long first, and taking care to read exactly that many bytes when reading, which takes a bit of care, especially on the last buffer. Another way is to send the length ahead of each buffer, sending zero after the last buffer. You can do all this with the methods of DataInputStream and DataOutputStream.

Similar Messages

  • I have created 2 websites one is in english and the other in russian how can i link these 2 so that on each page you can choose to read the page in russian or english??? like how do i put the two language flag toggles?

    like how do i put the two language flag toggles such that you can choose to read the site in russian or english?

    On each of the English pages add a Russian flag and link that flag to the Russian equivalent page with the Inspector/Link/Hyperlind pane.
    On each of the Russian pages add a US or British flag and link that flag to the equivalent English page.
    OT

  • How do I change the Not Editable flag of a variable?

    How do I change the Not Editable flag of a variable?  I need to edit the variable but right now the Not Editable box is checked.

    You can't do it unfortunately.  That's why it pops up a warning when you check it in the first place.
    Ok seriously though there is a workaround for this.  It's a total hack but may get you back to where you need to be without having to copy everything to a new sequence file. 
    WARNING:  Not sure what versions of TS this will work with.
    With the offending sequence file open go to Edit>>Sequence File Properties. 
    On the General tab change the File Format to XML. 
    Click OK. 
    Save the sequence file.
    Now browse to where you saved the sequence file and open it with Notepad.
    Search for FileGlobal and look for the offending variable. <MyGlobal classname='Num' valueflags='1'>
    It should have a property called valueflags.  It will be valueflags='1'.
    Remove that property altogether.  So it should be something like this: <MyGlobal classname='Num'>
    Save it and reopen it in TestStand.
    You may need to change it back to binary format from within TestStand.
    If deleting it gives you problems then just change the valueflags to 0.
    Hopefully this helps.
    jigg
    CTA, CLA
    teststandhelp.com
    ~Will work for kudos and/or BBQ~

  • Firewall: Error sending to the socket, server is not responding.

    Hi all,
    I'm trying to connect AIX machine to NT DB2 Database through JDBC with a firewall. I'm using the standard port 6789 in JDBC Applet Server in NT DB2. But I've gotten the following message:
    COM.ibm.db2.jdbc.DB2Exception: [IBM][JDBC Driver] CLI0614E Error sending to the socket, server is not responding. SQLSTATE=08S01
    The connection chain I'm using for AIX side is:
    jdbc:db2://192.168.3.4:6789/DATABASE
    I've configured the firewal to allow the port 6789 to be used, and also the other port 51544 (it's for remote administration, I think). I've also checked that wires and other stuff is working fine, but stil I cannot connect to the Database.
    I don't know if there's something missing on the firewall configuration. Before, everything was working without the firewall.
    Any help will be apreciated, THanks.
    Rodrigo, SPAIN

    It looks like no port problem is happening, because we freed all the ports just to see if client tried to use some of them without our knowing. But it was still the same.
    We were thinking about routing issues, but we were able to ping from client to server, and ports 6789 and 51544 were open as well. Maybe JDBC Client Driver is looking for an different IP address than 192.168.3.4. We know the firewall doesn't receive any query from JDBC Client, and that's very strange because if you see the routing tables in AIX machine, the default route is the firewall. Of course, there's not any other static defined route for the server (192.168.3.4), so it's supposed that default route is going to be used. But it's not.
    We also restarted client machine from scratch but nothing. Maybe this problem happens because there's something wrong in AIX networking settings.
    Rodrigo

  • Can anybody tell me how to send/receive  the sms from java application

    Hi All,
    Can any body tell me, how to send/receive the sms from java application to mobile phones.
    I have installed the jsms engine and when i try to connect to the mobile device ,the jsms server is giving
    the following error.
    Cannot connect to GSM Device, error : -11

    Which jsms? Google finds several.
    Try the website where you downloaded it.

  • Can any body tell me, how to send/receive the sms from java application

    Hi All,
    Can any body tell me, how to send/receive the sms from java application to mobile phones.
    I have installed the jsms engine and when i try to connect to the mobile device ,the jsms server is giving
    the following error.
    Cannot connect to GSM Device, error : -11

    The best place to ask your question is at the JSMS website, forum or mailing list since this is no error that directly comes from a class belonging to the core Java classes.

  • How can I clear the 'Index found' flag using Flexmotion 5.2?

    When using the 'find index' VI, the motor doesn't physically stop until a few steps after the index, even with the step rate dropped right down to 5 steps a second. (I have no idea whether this is normal or not - the axis is set to Halt on stop, so I would have thought not, but that's what it's doing!)
    This would be ok if I could clear the "index found" flag, drop the step rate down to one a second and do another index find in the opposite direction.
    How do I clear the Index found flag?
    Mike
    Mike Evans
    TRW Conekt
    N.I. Alliance Member, UK
    http://www.Conekt.net

    The "index found" flag will be cleared when you start a new flex_find_index command.

  • Can't install the software for the Brother MFC-9440CN because it is not currently available from the Software Update server....how do I get the driver then..it ised to work in my old mac..but cant print to it in my new unit

    Can't install the software for the Brother MFC-9440CN because it is not currently available from the Software Update server....how do I get the driver then..it ised to work in my old mac..but cant print to it in my new unit

    Download the Brother Mountain Lion drivers here.

  • How can I remove the Apple ID authorization only on one computer and authorize another in his place?

    how can I remove the Apple ID authorization only on one computer and authorize another in his place?

    De-authorize the computer in question.
    Then authorize the new computer.
    Or de-authorize all computers and authorize only the ones that actually exist.

  • I install LION on my mac pro 2008 and it's alway's pop with " there was a problem connecting to the server " Time Capsule" .How can i remove this popup. My Time machine is working fine and also rename it. But the popup keeps on coming with the old name.

    I installed LION on my mac pro 2008 and it's alway's pop with " there was a problem connecting to the server " Time Capsule" .How can i remove this popup. My Time machine is working fine and also rename it with less than 7 karakters. But the popup keeps on coming with the old name.

    I have a BT Infinity router plugged into the Time Capsule, not sure where the radio settings are?
    They are able to use the network settings of the TC i.e. they can connect to the internet via the wifi through the TC but when they try and connect to the AirPort Disk this is where it is not allowing a connection.
    I don;t have the drive shared out at all at the moment, is this necessary?  How do I do this if so?
    I have attached the screen shots of all the settings.
    Thanks again for your help.

  • How can I adjust the skin color of only one person in the photo (Elements 11)

    How can I adjust the skin color of only one person in the photo (Elements 11)?
    Thanks for your help.

    From your question, I assume you know how to do it for all persons :
    Menu : Enhance/Adjust color/adjust color for skin tones
    So what you have to do is to do the color correction on a layer copy. Concentrate on the person to enhance and ignore the rest of the picture.
    When you are satisfied, add a mask to the layer copy and fill it with black.
    Set your foreground color to white
    In the layer palette, click on the mask icon on the right of the layer icon.
    Then use a brush to paint white on the skin of the person to reveal the correction.

  • How can you get the keypad on a USB keyboard to work in Excel

    How can you get the keypad on a USB keyboard to work in Excel for Mac.  Mouse keys is disabled/unchecked, but cannot get it to work.  Even tried downloading a 'num lock' app and it didn't help.

    Hi Michael-
    I do not have any problem with using shift registers in timed loops.  What version of LabVIEW are you using?  Can you please elaborate on why neither shift registers or local variables meet your needs?  I have attached a VI that saves a previous iteration and then computes and displays the difference.  Let me know if you have problems running it.
    Thanks-
    Tom W
    National Instruments
    Attachments:
    TimedLoop_ShiftReg.vi ‏74 KB

  • Hi! I brought an iPhone 4 off my friend at work. The day I brought it, it was working fine but now I can't seem to send any messages or make phone calls. The network is showing up fine but each time I try to make a call it says "call failed" help any1?

    Hi! I brought an iPhone 4 off my friend at work. The day I brought it, it was working fine but now I can't seem to send any messages or make phone calls. The network is showing up fine but each time I try to make a call it says "call failed" I can't seem to send any messages either? I can use the Internet fine and everything else on the phone. Can anyone help?

    Hi chelsinator,
    You can reset the phone and it won't cause any problems or cause you to lose any data - Hold down the Home and Power buttons at the same time and continue to hold them down until the Silver Apple appears.
    The other thing you should do is to reset your network settings: Settings>General>Reset>Reset Network Settings. You will have to set up your Wifi again, but that oftentimes clears things up.
    Cheers,
    GB

  • I own the 4s and my BT Planttronics Pro works fine with calls but does not work to listen to music? Any ideas on how to correct this?

    I own the 4s and my BT Planttronics Pro works fine with calls but does not work to listen to music?
    Any ideas on how to correct this?
    Thanks-Vitaleyes

    I don't know what they call it, but my plantronics voyager pro, and my wifes plantronics voyager pro+ will not let you hear music through the headset.  My Blueant T1 does support it and plays the music through the deadset.

  • I have inadvertently synced two mail accounts so I now get my wifes and mine, how do i undue the action so I only receive mine?

    I have inadvertently synced two mail accounts so I now get my wifes and mine, how do I undue the action so I only receive mine?

    Remove your wife's account in Mail.

Maybe you are looking for

  • How to open a new IE page when user clicks on a button or link

    Hi Experts, I have created WD program which fetches data from R/3. I have to create a link or button on the WD program. When the user will click on the link, another IE page should open. Some processing will be carried out by WD. The result of WD pro

  • Filtering random pulses on a FP-SG-140

    Hello, I am a bit of a newbie when it comes to filtering , I have tested using pt-pt averaging and median filters but I am hoping that I can reduce noise on the output a little more. My system consists of using a FP-SG-140, FP-RTD-124.  I am doing lo

  • Excel generation

    Hi all, I created an excel sheet  using openxml and i wanted to retrieve list items and corresponding permission levels into excel.Iam using client object model.But iam able to get single item not all items in a list and iam not able to bind permissi

  • My ipod froze in the middle of updating to new iOS 6

    and i did the hold lock and home button thinking it will make it faster. it turned my ipod off but it didnt go to my home screen. it went to white. i held the buttons again now it went to a different color. currently the screen is green. please help.

  • Dump at select stmt

    the program is dumping at the below stmt. *--  Check whether any entry is present in the the table BSIP   SELECT * INTO LS_BSIP FROM BSIP           WHERE (LT_SEL_DATA). LT_SEL_DATA is declared as   TYPES : BEGIN OF T_SEL_DATA,             FIELD(72) T