How to Transfer a whole directory to other machines using socket programming?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Aravind_Thangaraj
Posted On:   Saturday, September 27, 2003 11:47 AM

I want to transfer a directory containing some set of of files to the clients usng TCP/IP sockets. For this I open a File object for that directory and store all the files in the directory to a File object array. I transfer the files in the File array to the remote client. I am sending number of files in the directory to the clients. The client and serrver are receive and send those files using the For loop, for the number of files times. Now, the problem is, in the for loop of the file transfer I first send the file name to the client and then data content of the files. After transfering one file successfully, the socket connection was automatically closed.   More>>
			
I want to transfer a directory containing some set of
of files to the clients usng TCP/IP sockets. For this
I open a File object for that directory and store all the
files in the directory to a File object array.
I transfer the files in the File array to the remote
client. I am sending number of files in the directory to
the clients. The client and serrver are receive and send
those files using the For loop, for the number of files
times.
Now, the problem is, in the for loop of the file transfer
I first send the file name to the client and then data
content of the files. After transfering one file
successfully, the socket connection was automatically
closed. I didn't close the socket anywhere and even the
streams also.
Here with I added the code snipset for u'r reference.
Can you explain, where the problem is occured and how to
rectify that?
Thanks in advance.
with regards,
aravind.t

Sample code:
------------




			

Server:
-------

File img = new File("ans");
File[] files;
if(img.isDirectory())
{
PrintWriter out = new PrintWriter(client.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
FilenameFilter filter = new only(".jpg");
files = img.listFiles(filter);
int num = files.length;
System.out.println("Number of files in the directory: "+num+"
Files:");

for(int i=0;i
System.out.println(files[i].getAbsolutePath());

out.println(num);

for(int i=0;i < num; i++)
{
out.println(files[i].getName());
out.flush();
System.out.print ( "Transfering Image file < " + files[i].getName () +
" > to Client: [ " + client.getInetAddress () + " ] : " );
boolean trans =File_Transfer(files[i].getAbsolutePath());
// Transfer.Transfer(client.getOutputStream(), files[i] );
System.out.println ( trans ? "Success" : "Failure" );
synchronized(this){
System.out.println("Client: "+in.readLine());
}
}
}
}catch(IOException e)
{
System.out.println("Error in Socket: "+e);
}
}

private static boolean File_Transfer(String fn){
FileInputStream fis;
BufferedOutputStream bos;

try {
fis = new FileInputStream ( new File ( fn ) );
bos = new BufferedOutputStream ( client.getOutputStream () );
byte rd[] = new byte[ 100 ];
while ( fis.read ( rd ) != -1 ) {
bos.write ( rd );
bos.flush ();
}
bos.close ();
fis.close ();
}
catch ( IOException e ) {
System.out.println ( "Error:" + e );
return false;
}
return true;
}



Client:
-------


try {
client = new Socket ( "myhost", 2000 );
System.out.println ( "Client Connected with the Server ..." );
BufferedReader in;
in = new BufferedReader ( new InputStreamReader ( client.getInputStream () ) );
PrintWriter out = new PrintWriter ( client.getOutputStream (),true );
String str = in.readLine ();
System.out.println ( str );
int num = Integer.parseInt ( str );
System.out.println ( "Number of files in the Directory: " + num );
File temp = new File ( "MyDir" );
temp.mkdir ();
for ( int k = 0; k < num; k++ ) {
String fn = in.readLine ();
System.out.println("Receiving File: "+fn);
File img = new File ("MyDir/"+fn );
byte byt[] = new byte[ 100 ];
{
BufferedInputStream bis = new BufferedInputStream (
client.getInputStream () );
FileOutputStream fos = new FileOutputStream ( img, true );
while ( bis.read ( byt ) != -1 )
{
fos.write ( byt );
fos.flush();
}
fos.close ();
//bis.close ();
out.println ( "Received" );
out.flush ();
System.out.println ( fn + " ---> File Received." );
}
}
}
catch ( IOException e ) {
e.printStackTrace ();
}
   <<Less

Re: How to Transfer a whole directory to other machines using socket programming?

Posted By:   Anonymous  
Posted On:   Friday, October 10, 2003 09:52 AM


Try taking the bis declaration out of the for loop. It's getting disposed when you create a "new" one, but it's really the same stream.


Current code:


for ( int k = 0; k < num; k++ ) {
String fn = in.readLine ();
System.out.println("Receiving File: "+fn);
File img = new File ("MyDir/"+fn );
byte byt[] = new byte[ 100 ];
{
BufferedInputStream bis = new BufferedInputStream (...);
:
:
}
}


Change that to:


BufferedInputStream bis = new BufferedInputStream (...);

for ( int k = 0; k < num; k++ ) {
String fn = in.readLine ();
System.out.println("Receiving File: "+fn);
File img = new File ("MyDir/"+fn );
byte byt[] = new byte[ 100 ];
{
:
:
}
}

Re: How to Transfer a whole directory to other machines using socket programming?

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, September 29, 2003 06:31 AM

Perhaps you should make a zip archive of the whole directory structure first, transfer the zip archive, and then extract at the other end.

About | Sitemap | Contact