ftp data connection refusal
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Sunday, February 15, 2004 11:49 PM

i've tried to work out a ftp client. I successfully done it with direct modem connection. but when i tried it thro' proxy server, i cannot establish a data connection to the server. the problem is illegal port command. when i entered the port command with hands and sent out, it works fine . but i cannot include it in my program. please tell me what may be the problem. for ur convenience i've attached both of the programs 1. commands entered by hands and 2. automatic connection (i've tried it to quick downloader) import java.io.*; import java.net.*; class ftptodo{ public static void main(String args[]) throws Exception { URL url=new URL(args[0]); Socket cs=new Socket(InetAddress.getByN   More>>

i've tried to work out a ftp client. I successfully done it with direct modem connection. but when i tried it thro' proxy server, i cannot establish a data connection to the server. the problem is illegal port command.
when i entered the port command with hands and sent out, it works fine . but i cannot include it in my program. please tell me what may be the problem. for ur convenience i've attached both of the programs 1. commands entered by hands and 2. automatic connection (i've tried it to quick downloader)

			
import java.io.*;
import java.net.*;
class ftptodo{
public static void main(String args[]) throws Exception
{
URL url=new URL(args[0]);
Socket cs=new Socket(InetAddress.getByName(url.getHost()),21);
BufferedInputStream is=new BufferedInputStream(cs.getInputStream());
PrintWriter os=new PrintWriter(cs.getOutputStream(),true);
DataInputStream dis=new DataInputStream(System.in);
ftptodo ft=new ftptodo();
ft.new inp(is);
ft.new receive();
System.out.println("Local IP : "+cs.getLocalAddress());
String output=new String("NOOP");
while(!output.equals("end"))
{
if((output=dis.readLine())!=null)
os.println(output);
}
}
class receive extends Thread{
ServerSocket ss=null;
receive(){
try{
ss=new ServerSocket(2048);
start();
}
catch(Exception e)
{e.printStackTrace();}
}
public void run()
{
try{
System.out.println("dataport "+ss.getLocalPort());
Socket cs=ss.accept();
BufferedReader bis=new BufferedReader(new InputStreamReader(cs.getInputStream()));
String data=null;
BufferedWriter bw=new BufferedWriter(new FileWriter("test.tst"));
while((data=bis.readLine())!=null)
{
bw.write(data,0,data.length());
}
bw.close();
}
catch(Exception e)
{e.printStackTrace();
}
}
}
class inp extends Thread{
BufferedReader is=null;
inp(BufferedInputStream is)
{
this.is=new BufferedReader(new InputStreamReader(is));
start();
}
public void run()
{
String input=null;
try{
while(true)
{
if((input=is.readLine())!=null)
System.out.println("From server "+input);
}
}
catch(Exception e)
{e.printStackTrace();}
}
}
}

2.

import java.io.*;
import java.net.*;
class qcknewftp
{
public static long leng;
public static String hostname;
public static int noofthreads;
public static int dataport=2048;
public static URL url;
public static long time=0;
public static void main(String args[]) throws Exception
{
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter The URL(ftp)");
url=new URL(din.readLine());
hostname=url.getHost();
System.out.println("Enter the number of threads");
noofthreads=Integer.parseInt(din.readLine());
time=System.currentTimeMillis();
URLConnection uc=url.openConnection();
leng=uc.getContentLength();
System.out.println(leng+" - length of file");
int i=0;
qcknewftp qf=new qcknewftp();
while(i
{
qf.new down(i);
i++;
}
System.out.println("all are connected at "+(System.currentTimeMillis()-time));
}
class down extends Thread
{
int thnum;
String status;
long start;
String ip2;
PrintWriter pw;
down(int i)
{
ip2=new String("");
this.thnum=i;
this.status=new String("start");
this.start();
}
public void run()
{
try
{
Socket s=new Socket(InetAddress.getByName(hostname),21);
System.out.println(thnum+" connected");
pw=new PrintWriter(s.getOutputStream(),true);
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
String fromserver,toserver;
this.new getinput(br);
pw.println("USER anonymous");
pw.println("PASS raja@yahoo.com");
pw.println("TYPE I");
start=(leng/noofthreads)*thnum;
long limit=(leng/noofthreads)*(thnum+1);
ServerSocket ss=new ServerSocket(dataport++);
String ip=s.getLocalAddress().toString();
int port =ss.getLocalPort();
char tc,prechar='2';
boolean recchar=false;
System.out.println("Ip connected"+ip);
for(int j=0;j
{
tc=ip.charAt(j);
if(tc=='/')
recchar=true;
if(!recchar)
continue;
if(tc=='.')
tc=',';

if(tc=='0'||tc=='1'||tc=='2'||tc=='3'||tc=='4'||tc=='5'||tc=='6'||tc=='7'||tc=='8'||tc=='9'||tc==',')
ip2+=String.valueOf(tc);
prechar=tc;
}
System.out.println("ip to server"+ip2+"-"+thnum);
ip2+=',';
String por=String.valueOf((port/1024)*4);
ip2+=por;
ip2+=",";
ip2+=String.valueOf((port%1024));
System.out.println("ip"+ip2+"end");
pw.println("port "+ip2);
this.new datasocket(ss,limit);
pw.println("REST "+String.valueOf(start));
pw.println("RETR "+url.getFile());
}
catch(Exception e)
{e.printStackTrace();}
}
class datasocket extends Thread
{
ServerSocket sso;
long limit;
datasocket(ServerSocket so,long limit)
{
this.sso=so;
this.limit=limit;
this.start();
}
public void run()
{
try
{
long count=start;
System.out.println("yet to connect"+thnum);
Socket daso=sso.accept();
System.out.println("Data socket connected for thread no "+String.valueOf(thnum));
BufferedInputStream isd=new BufferedInputStream(daso.getInputStream());
FileOutputStream fout=new FileOutputStream("tempfile"+String.valueOf(thnum)+".txt");
int data;
data=isd.read();
if(thnum==(noofthreads-1))
while(data!=-1)
{
System.out.println("upto end of file");
fout.write(data);
data=isd.read();
count++;
if(count%512==0)
System.out.println("written"+count+"bytes at thno"+thnum);
}
else
while(count
{
fout.write(data);
data=isd.read();
count++;
if(count%512==0)
System.out.println("written"+count+"bytes at thno"+thnum);
}
status="end";
System.out.println((System.currentTimeMillis()-time)+" - time for thread"+thnum);
fout.close();
daso.close();
isd.close();
}
catch(Exception e)
{e.printStackTrace();}
}
}
class getinput extends Thread
{
BufferedReader br;
getinput(BufferedReader br)
{
this.br=br;
this.start();
}
/*public boolean inseq(int replycode,int prereplycode)
{
int codearray={220,331,230,350*/
public void run()
{
String servertext;
System.out.println("input from server");
try
{
int replycode,prereplycode;
BufferedWriter bw=new BufferedWriter(new FileWriter("replies"+String.valueOf(thnum)+".txt"));
while(true)
{
servertext=br.readLine();
replycode=Integer.parseInt(servertext.substring(0,2));
if(replycode==500)
pw.println("PORT "+ip2);
bw.write(servertext);
System.out.println(servertext);
if (status.equals("end"))
break;
}
bw.close();
br.close();
System.out.println("server replies closed - "+thnum);
}
catch(Exception e)
{e.printStackTrace();}
}
}
}
}
   <<Less

Re: ftp data connection refusal

Posted By:   Zac_Jacobson  
Posted On:   Tuesday, February 17, 2004 11:44 AM

About | Sitemap | Contact