ftp协议需要使用20,21两个port,服务器在传输命令时使用21,传输数据时使用20
当要进行传输时,客户端打开一个1024以上的随机端口,同时在已经和服务器建立连接的命令端口21上传输PORT命令,要求服务器使用20端口连接客户端刚打开的随机端口,服务器这才使用自己的20端口建立与客户端的连接,传送完文件后服务器将20端口关闭。你们的程序好像并没有完整解析ftp协议,没有取得客户端打开的随机端口号,所以不能建立主动的数据传输连接。当然也可以建立被动的数据传输连接,但是还是需要完整解析ftp协议。
由于网关对ip包进行了转发,你们没有取得客户的ip地址也是有可能的,具体要看你们的网关是哪一类网关。
当要进行传输时,客户端打开一个1024以上的随机端口,同时在已经和服务器建立连接的命令端口21上传输PORT命令,要求服务器使用20端口连接客户端刚打开的随机端口,服务器这才使用自己的20端口建立与客户端的连接,传送完文件后服务器将20端口关闭。你们的程序好像并没有完整解析ftp协议,没有取得客户端打开的随机端口号,所以不能建立主动的数据传输连接。当然也可以建立被动的数据传输连接,但是还是需要完整解析ftp协议。
由于网关对ip包进行了转发,你们没有取得客户的ip地址也是有可能的,具体要看你们的网关是哪一类网关。
解决方案 »
- java 题目 完成两个数组内容的交换 并输出
- 哪位大侠知道键盘全选事件
- 关于常量
- 关于客户端给服务端传参数问题~
- import怎么导入其他工程的类
- td里面 table要靠上贴着上面放怎么写?
- 关于 JAVA 就业前景的看法。。。。希望已经工作的大侠们,给点建议。
- 100分:从C++转入Java,做开源项目,几个简单问题兼见面礼
- 我用ant编译src目录下所有的java文件,(大约有1500个),运行后报java.lang.OutOfMemoryError,这是为什么?
- 怎么在web上运行applet?我已经把class和web文件放到同一目录下了,总体是我no found class
- 这段简单的代码怎么不能执行?
- 初学java,大家能否帮我看看下面这小段代码到底哪儿错了?
import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;public class FtpApplet extends Applet
{
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs;
TextArea lsArea;
Label LblPrompt;
Button BtnConn;
Button BtnClose;
TextField TxtUID;
TextField TxtPWD;
TextField TxtHost;
int ch;
public String a="没有连接主机";
String hostname="";
public void init () {
setBackground(Color.white);
setLayout(new GridBagLayout());
GridBagConstraints GBC = new GridBagConstraints();
LblPrompt = new Label("没有连接主机");
LblPrompt.setAlignment(Label.LEFT);BtnConn = new Button("连接");
BtnClose = new Button("断开");
BtnClose.enable(false);
TxtUID = new TextField("",15);
TxtPWD = new TextField("",15);
TxtPWD.setEchoCharacter('*');
TxtHost = new TextField("",20);
Label LblUID = new Label("User ID:");
Label LblPWD = new Label("PWD:");
Label LblHost = new Label("Host:");lsArea = new TextArea(30,80);
lsArea.setEditable(false);GBC.gridwidth= GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
((GridBagLayout)getLayout()).setConstraints(LblPrompt,GBC);
add(LblPrompt);GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblHost,GBC);
add(LblHost);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(TxtHost,GBC);
add(TxtHost);GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblUID,GBC);
add(LblUID);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(TxtUID,GBC);
add(TxtUID);GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblPWD,GBC);
add(LblPWD);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(TxtPWD,GBC);
add(TxtPWD);GBC.gridwidth=1;
GBC.weightx=2;
((GridBagLayout)getLayout()).setConstraints(BtnConn,GBC);
add(BtnConn);
GBC.gridwidth=GridBagConstraints.REMAINDER;((GridBagLayout)getLayout()).setConstraints(BtnClose,GBC);
add(BtnClose);GBC.gridwidth=GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
((GridBagLayout)getLayout()).setConstraints(lsArea,GBC);
add(lsArea);
}public boolean connect(String hostname, String uid,String pwd)
{
this.hostname = hostname;
LblPrompt.setText("正在连接,请等待.....");
try{
aftp =new FtpClient(hostname);
aftp.login(uid,pwd);
aftp.binary();
showFileContents();
}
catch(FtpLoginException e){
a="无权限与主机:"+hostname+"连接!";
LblPrompt.setText(a);
return false;
}
catch (IOException e){
a="连接主机:"+hostname+"失败!";
LblPrompt.setText(a);
return false;
}
catch(SecurityException e)
{
a="无权限与主机:"+hostname+"连接!";
LblPrompt.setText(a);
return false;
}
LblPrompt.setText("连接主机:"+hostname+"成功!");
return true;
}public void stop()
{
try
{
aftp.closeServer();
}
catch(IOException e)
{
}
}public void paint(Graphics g){
}public boolean action(Event evt,Object obj)
{
if (evt.target == BtnConn)
{
LblPrompt.setText("正在连接,请等待.....");
if (connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText()))
{
BtnConn.setEnabled(false);
BtnClose.setEnabled(true);
}
return true;
}
if (evt.target == BtnClose)
{
stop();
BtnConn.enable(true);
BtnClose.enable(false);
LblPrompt.setText("与主机"+hostname+"连接已断开!");
return true;
}
return super.action(evt,obj);
}
public boolean sendFile(String filepathname){
boolean result=true;
if (aftp != null)
{
LblPrompt.setText("正在粘贴文件,请耐心等待....");String contentperline;
try{
a="粘贴成功!";
String fg =new String("\\");
int index = filepathname.lastIndexOf(fg);
String filename = filepathname.substring(index+1);
File localFile ;
localFile = new File(filepathname) ;
RandomAccessFile sendFile = new RandomAccessFile(filepathname,"r");
//
sendFile.seek(0);
outs = aftp.put(filename);
outputs = new DataOutputStream(outs);
while (sendFile.getFilePointer() < sendFile.length() )
{
ch = sendFile.read();
outputs.write(ch);
}
outs.close();
sendFile.close();
}
catch(IOException e){
a = "粘贴失败!";
result = false ;}
LblPrompt.setText(a);
showFileContents();
}
else{
result = false;
}
return result;
}public void showFileContents()
{
StringBuffer buf = new StringBuffer();
lsArea.setText("");
try
{
ins= aftp.list();
while ((ch=ins.read())>=0){
buf.append((char)ch);
}
lsArea.appendText(buf.toString());
ins.close();
}
catch(IOException e)
{
}
}
public static void main(String args[]){
Frame f = new Frame("FTP Client");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
} });
FtpApplet ftp = new FtpApplet();
ftp.init();
ftp.start();
f.add(ftp);
f.pack();
f.setVisible(true);
}
}
Resource URL : http://www.mycgiserver.com/~ranab
Source Code : http://www.mycgiserver.com/~ranab
Detailed Review : Not Available
Type : Other
Category : Programming - Other
Language : Java
Marketing : Freeware - Beta
History : New Submission
Version : 1.0
Date Submitted : Mon Jul 17 18:30:49 2000