View Single Post
  #1 (permalink)  
Old 05-19-2008, 01:53 PM
rameshraj rameshraj is offline
Member
 
Join Date: Dec 2007
Posts: 38
rameshraj is on a distinguished road
Sending files over sockets!
While sending files over sockets,it is ok for small files like text and java.When some larger files like .pdf,.wmv..mp3 etc are sent then the file received is corrupted on the receiving end and the file size is also greater than the sent one.Also the receiving end is waiting infinitely to write.
Actually what might have been happening?Can anyone give me some idea?

The receiving end has the receiving code as shown below:

InputStream in = clientSocket.getInputStream();
byte[] buf = new byte[Integer.parseInt(headers[2])];//allocate the size as //the number of bytes available on the stream
//to get the filename and file extensions
String []fileExtension=headers[1].split("\\.");//split on basis of dot to get the

//file extension
//the last content of the filedetails array will be the extension

String f2=headers[1];//+"."+fileExtension[fileExtension.length-1];
//or
f2="E:/E-resources/java/Transfer."+fileExtension[fileExtension.length-1];
//delete already existing file with same name
if(new File(f2).exists())
{
//new File(f2).delete();
File file=new File(f2);
System.out.println("File Deleted\t"+file.delete());
}
System.out.println("Writing as a file\t"+f2);
FileOutputStream fos = new FileOutputStream(new File(f2));
//int size=in.available();

while(in.read(buf)>=0)
{
fos.write(buf);
System.out.println("Input streams available are\t"+in.read(buf));

}
fos.close();//close the fileoutputStream finally
in.close();
Reply With Quote
Sponsored Links