java.net.SocketException: socket closed

hi all

i'm having a problem in socket programming. well, through the code, i'm trying to download a file...and at the end of the download i get the following error:

java.net.SocketException: socket closed

java.io.EOFException

at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2502)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)

at beans.downloadingThread.run(Download.java:79)

java.net.SocketException: socket closed

the code is as follows:

try{

//set up our connection

connect =new Socket (IPAddress, 1000);

ObjectOutputStream outclient =new ObjectOutputStream(connect.getOutputStream());

ObjectInputStream infrom =new ObjectInputStream(connect.getInputStream());

//write the objects of stuff to get to the server

outclient.writeObject(fileGet);

//set our progress bars minimum

progress.setMinimum(0);

//setup the progress bars maximum

progress.setMaximum(Integer.parseInt(size.toString()));

//start the while loop

while (true){

//setup the get varible to read from the server

Object get = infrom.readObject();

//switch the flag so our in from server can have some sense made of them

switch (flag)

{

//case 0 is our main case this is when things get triggered

case 0:

//see if we got a start download;

if (get.equals(".startdownload"))

flag = 1;

case 1:

//our in is the same as our out BUT we have to read from the stream byte by byte

int data;

int totalDataRead;

int totalSizeWritten = 0;

int totalSizeRead;

int PACKET_SIZE=125;

byte[] packet=newbyte[PACKET_SIZE];

try{

String currentDirectory = System.getProperty("user.dir");

String uploaddirectory = currentDirectory +"\\share1\\"+fileGet;

FileOutputStream fos =new FileOutputStream(uploaddirectory);

long file_size=Integer.parseInt(size.toString());

long no_of_packet=file_size/125;

int last_psize=(int)(file_size-(no_of_packet*125));

while((totalDataRead = infrom.read(packet,0,packet.length)) >-1){

fos.write(packet,0,totalDataRead);

totalSizeWritten = totalSizeWritten + totalDataRead;

progress.setValue(totalSizeWritten);

}

}

}

catch(Exception x){

System.err.println(x);

x.printStackTrace();

}

flag = 0;

}fos.close();

}

}catch (Exception e){

System.err.println(e);

e.printStackTrace();

}

i've checked the downloaded file and the file is downloaded completely and properly. but still by the end of the download process it indefinitely prints the error

java.net.SocketException: socket closed

Please, help!

Thanks

[4602 byte] By [priyankaSa] at [2007-11-27 1:53:35]
# 1
somebody...anybody....please help me...i'm stuck up in my codeits urgent
priyankaSa at 2007-7-12 1:24:00 > top of Java-index,Core,Core APIs...
# 2

Hello,

I think the issue might be that the server closes the connection after sending the file, while the client (your code) does not close the socket or socket streams.

Can you make sure that the server does not close the connection after sending the file and if the server does close the connection, I guess you'll have to close your socket as well.

Also, before retrieving an InputStream (certainly if it is an ObjectInputStream) or before reading from it you should have flushed the OutputStream at the other side of the connection. An InputStream tends to block until you flush the corresponding OutputStream.

Is the inner try aligned with the inner catch? It appears that the inner try is encapsulated within the switch statement, while the inner catch is outside the switch statement (or do I see things wrong?)

Are you sure that no_of_packet = file_size/125? An integer division rounds down, so you won't calculate the remainder.

What is fileGet and how does its value change in the loop?

I am asking some questions so you can think about them, but it is not completely clear to my what you want to do and why you want to do it this way.

toon_macharisa at 2007-7-12 1:24:00 > top of Java-index,Core,Core APIs...
# 3

After the file is read, it just loops back and tries to read "get" again, even though the file reading only stops when the stream is cleared.

A few other notes:

-"flag", "get" and "last_psize" aren't actually used. For one thing, you can remove the whole "switch", and also the filesize->packet number calculation(unless you want to switch the ending condition).

-The code as you posted it would not compile. There are several errors in it, like one of the "catch"s leaning on the "switch" rather than the "try". If you'll count you'll notive "{" appears 7 times and "}" 8 times.

-The first "while(true)" is a big problem, if I'm getting where the extra "}" is okay, because after it loops the socket will always be closed, and it will attempt to receive "get" again.

So:

1. Reduce the flow control. Reduce it ALOT. You can get the same functionality with just a couple of "if"s.

2. Unless you like Exceptions, alter the protocol to use raw bytes only(note that anything can be encoded to raw bytes, so you're not losing any functionality) and implement it as such, making all of the conversions manually(for strings, use getBytes, and you can even pick your encoding).

3. Test for stream end at any point.

4. Add a bit of general error testing to the input. Count your blessings that you can't create a buffer overrun security flaw with Java(It would throw something before it allows that).

SlugFillera at 2007-7-12 1:24:00 > top of Java-index,Core,Core APIs...