Skip to Main Content

Java Programming

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!

java.net.SocketException: socket closed

807606Apr 21 2007 — edited Apr 23 2007
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 (ipAddy, 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=new byte[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!
Its URGENT.
Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2007
Added on Apr 21 2007
9 comments
1,525 views