Skip to content

Commit

Permalink
Merge pull request #8 from hxcan/hxcan-patch-1
Browse files Browse the repository at this point in the history
Add files via upload
  • Loading branch information
hxcan authored Sep 21, 2022
2 parents 45275cd + 40ccf1f commit 90c11ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ControlConnectHandler
private byte[] dataSocketPendingByteArray=null; //!< 数据套接字数据内容 排队。
private String currentWorkingDirectory="/"; //!< 当前工作目录
private int data_port=1544; //!< 数据连接端口。
private String ip; //!< ip
private boolean allowActiveMode=true; //!< 是否允许主动模式。
private File writingFile; //!< 当前正在写入的文件。
private boolean isUploading=false; //!< 是否正在上传。陈欣
Expand Down Expand Up @@ -102,16 +103,17 @@ private void receiveDataSocket( ByteBufferList bb)
{
e.printStackTrace();
}
} //private void receiveDataSocket( ByteBufferList bb)
} // private void receiveDataSocket( ByteBufferList bb)

public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host)
public ControlConnectHandler(Context context, boolean allowActiveMode, InetAddress host, String ip)
{
this.context=context;
this.allowActiveMode=allowActiveMode;
this.host=host;
this.ip=ip; // Remember ip for data server.

setupDataServer(); // 启动数据传输服务器。
}
setupDataServer(); // 启动数据传输服务器。
}

/**
* 打开指向客户端特定端口的连接。
Expand Down Expand Up @@ -386,16 +388,22 @@ else if (command.equals("TYPE")) // 传输类型
else if (command.equals("PASV")) // 被动传输
{
setupDataServer(); // 初始化数据服务器。

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());

String ip = ipAddress.replace(".", ",");

String ipAddress = ip;


if (ipAddress==null) // Have not set ip.
{
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
ipAddress = Formatter.formatIpAddress(wifiManager.getConnectionInfo().getIpAddress());
} // else // Not set ip.

String ipString = ipAddress.replace(".", ",");

int port256=data_port/256;
int portModule=data_port-port256*256;

String replyString="227 Entering Passive Mode ("+ip+","+port256+","+portModule+") "; // 回复内容。
String replyString="227 Entering Passive Mode ("+ipString+","+port256+","+portModule+") "; // 回复内容。

Log.d(TAG, "reply string: " + replyString); //Debug.

Expand Down Expand Up @@ -941,9 +949,6 @@ public void onCompleted(Exception ex)
}
});

//发送初始命令:
// send_data "220 \n"

binaryStringSender.sendStringInBinaryMode("220 StupidBeauty FtpServer"); // 发送回复内容。
} //private void handleAccept(final AsyncSocket socket)

Expand All @@ -958,8 +963,6 @@ private void setupDataServer()

data_port=randomIndex;

// try // 绑定端口。
// {
AsyncServer.getDefault().listen(host, data_port, new ListenCallback()
{
@Override
Expand All @@ -979,9 +982,6 @@ public void onCompleted(Exception ex)
{
if(ex != null)
{
// 09-07 07:57:47.473 18998 19023 W System.err: java.lang.RuntimeException: java.net.BindException: Address already in use

// throw new RuntimeException(ex);
ex.printStackTrace();

setupDataServer(); // 重新初始化。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class FtpServer
private static final String TAG="FtpServer"; //!< 输出调试信息时使用的标记
private InetAddress host;
private int port;
private String ip; //!< ip
private boolean allowActiveMode=true; //!< 是否允许主动模式。
private File rootDirectory=null; //!< 根目录。

Expand All @@ -55,18 +56,24 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)

public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener)
{
this(host, port, context, allowActiveMode, errorListener, null);
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)

public FtpServer(String host, int port, Context context, boolean allowActiveMode, ErrorListener errorListener, String externalIp)
{
this.context=context;
this.allowActiveMode=allowActiveMode;
this.errorListener=errorListener; // 记录错误事件监听器。

this.ip=externalIp; // Remember the external ip.

rootDirectory=context.getFilesDir(); // 默认在家目录下工作。
try

try
{
this.host = InetAddress.getByName(host);
}
catch (UnknownHostException e)
catch (UnknownHostException e)
{
throw new RuntimeException(e);
}
Expand All @@ -75,7 +82,7 @@ public FtpServer(String host, int port, Context context, boolean allowActiveMode

setup();
} //public FtpServer(String host, int port, Context context, boolean allowActiveMode)

/**
* Set user manager.
*/
Expand All @@ -91,7 +98,7 @@ private void setup()
@Override
public void onAccepted(final AsyncSocket socket)
{
ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host); // 创建处理器。
ControlConnectHandler handler=new ControlConnectHandler(context, allowActiveMode, host, ip); // 创建处理器。
handler.handleAccept(socket);
handler.setRootDirectory(rootDirectory); // 设置根目录。
handler.setEventListener(eventListener); // 设置事件监听器。
Expand Down

0 comments on commit 90c11ac

Please sign in to comment.