RE: [JSch-users] Problem: too many open files with scp
Status: Alpha
Brought to you by:
ymnk
From: Thomas <pha...@ya...> - 2006-02-17 23:28:00
|
A complication is that I am invoking scp to/from from within a thread. I first created my own Scp class with two methods scpTo() and scpFrom(), which are cut-and-pasted from the examples. Each of these two methods creates a new Jsch object, gets a session, etc. The scpFrom() code is shown below. Then, each of my threads can call Scp.scpTo() and Scp.scpFrom() many times to do downloading and then uploading for the same host/server combination. If I do session.disconnect() at the end of scpTo(), I get an exception: java.lang.RuntimeException: java.net.SocketException: Socket closed at com.jcraft.jsch.Session.run(Session.java:1212) at java.lang.Thread.run(Thread.java:567) Caused by: java.net.SocketException: Socket closed at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.read(SocketInputStream.java:153) at com.jcraft.jsch.IO.getByte(IO.java:71) at com.jcraft.jsch.Session.read(Session.java:665) at com.jcraft.jsch.Session.run(Session.java:948) Here is my code for scpFrom(). Can someone check if I have closed and disconnected everything properly? public void scpFrom(String remote_filename, String local_filename, String user, String password, String host) throws Exception { String prefix=null; if(new File(local_filename).isDirectory()) { prefix=local_filename+File.separator; } JSch jsch=new JSch(); Session session=jsch.getSession(user, host, 22); // username and password will be given via UserInfo interface. /* UserInfo ui=new MyUserInfo(); */ UserInfo ui = new DummyUserInfo(password); session.setUserInfo(ui); try { session.connect(); } catch(Exception ex) { throw (new ScpConnectionException()); } // exec 'scp -f remote_filename' remotely String command="scp -f "+remote_filename; Channel channel=session.openChannel("exec"); ((ChannelExec)channel).setCommand(command); // get I/O streams for remote scp OutputStream out=channel.getOutputStream(); InputStream in=channel.getInputStream(); channel.connect(); byte[] buf=new byte[_recv_buffer_size_in_bytes]; // send '\0' buf[0]=0; out.write(buf, 0, 1); out.flush(); while(true) { int c=checkAck(in); if (c!='C') { break; } // read '0644 ' in.read(buf, 0, 5); int filesize=0; while(true) { in.read(buf, 0, 1); if(buf[0]==' ')break; filesize=filesize*10+(buf[0]-'0'); } String file=null; for(int i=0;;i++) { in.read(buf, i, 1); if(buf[i]==(byte)0x0a) { file=new String(buf, 0, i); break; } } // send '\0' buf[0]=0; out.write(buf, 0, 1); out.flush(); // read a content of local_filename FileOutputStream fos=new FileOutputStream(prefix==null ? local_filename : prefix+file); int foo; while(true) { if(buf.length<filesize) foo=buf.length; else foo=filesize; in.read(buf, 0, foo); fos.write(buf, 0, foo); filesize-=foo; if(filesize==0) break; } fos.close(); //fos = null; byte[] tmp=new byte[1]; if(checkAck(in)!=0){ //System.exit(0); throw (new ScpGeneralException()); } // send '\0' buf[0]=0; out.write(buf, 0, 1); out.flush(); } // while out.close(); in.close(); channel.close(); session.disconnect(); session = null; channel = null; //System.exit(0); } // scpFrom __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com |