博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android ftp 服务器
阅读量:7105 次
发布时间:2019-06-28

本文共 3989 字,大约阅读时间需要 13 分钟。

源码地址:   git clone git://github.com/ppareit/swiftp

1、获取手机的wifi 地址作为 ftp 服务器的地址

public static InetAddress getWifiIp()    {        Context myContext = Globals.getContext();        if (myContext == null)        {            throw new NullPointerException("Global context is null");        }        WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);        if (isWifiEnabled())        {            int ipAsInt = wifiMgr.getConnectionInfo().getIpAddress();            if (ipAsInt == 0)            {                return null;            }            else            {                return Util.intToInet(ipAsInt);            }        }        else        {            return null;        }    }    public static boolean isWifiEnabled()    {        Context myContext = Globals.getContext();        if (myContext == null)        {            throw new NullPointerException("Global context is null");        }        WifiManager wifiMgr = (WifiManager) myContext.getSystemService(Context.WIFI_SERVICE);        if (wifiMgr.getWifiState() == WifiManager.WIFI_STATE_ENABLED)        {            return true;        }        else        {            return false;        }    }

2、创建服务器套接字 listenSocket, 端口号可以自定义

listenSocket = new ServerSocket();        listenSocket.setReuseAddress(true);        listenSocket.bind(new InetSocketAddress(port));

3、用子线程TcpListener封装 listenSocket

while (!shouldExit)        {            if (acceptWifi)            {                if (wifiListener != null)                {                    if (!wifiListener.isAlive())                    {                        myLog.l(Log.DEBUG, "Joining crashed wifiListener thread");                        try                        {                            wifiListener.join();                        }                        catch (InterruptedException e)                        {                        }                        wifiListener = null;                    }                }                if (wifiListener == null)                {                    // Either our wifi listener hasn't been created yet, or has                    // crashed,                    // so spawn it                    wifiListener = new TcpListener(listenSocket, this);                    wifiListener.start();                }            }}

 

4、listensocket 在TcpListener 线程的run 方法中循环监听客户端请求,并且将建立链接的clientSocket 封装到会话线程

public void run()    {        try        {            while (true)            {                Socket clientSocket = listenSocket.accept();                myLog.l(Log.INFO, "New connection, spawned thread");                SessionThread newSession = new SessionThread(clientSocket, new NormalDataSocketFactory(), SessionThread.Source.LOCAL);                newSession.start();                ftpServerService.registerSessionThread(newSession);            }        }        catch (Exception e)        {            myLog.l(Log.DEBUG, "Exception in TcpListener");        }    }

5、在会话进程的run方法中循环处理客户端都请求

public void run()    {        try        {            BufferedReader in = new BufferedReader(new InputStreamReader(cmdSocket.getInputStream()), 8192);            while (true)            {                String line;                line = in.readLine(); // will accept \r\n or \n for terminator                if (line != null)                {                    FTPServerService.writeMonitor(true, line);                    myLog.l(Log.DEBUG, "Received line from client: " + line);                    FtpCmd.dispatchCommand(this, line);                }                else                {                    myLog.i("readLine gave null, quitting");                    break;                }            }        }        catch (IOException e)        {            myLog.l(Log.INFO, "Connection was dropped");        }        closeSocket();    }

6、FtpCmd是所有命令类的父抽象类,在dispatchCommand方法中根据输入流中的参数判断命令类型,然后执行对应的命令

转载于:https://www.cnblogs.com/lipeil/archive/2012/07/28/2613126.html

你可能感兴趣的文章
深入浅出多线程系列之一:简单的Thread(转)
查看>>
mysql优化学习(一)
查看>>
java 配置及Eclipse安装
查看>>
0427Python基础-运算符-编码
查看>>
linux + qt 环境搭建
查看>>
如何选择Html.RenderPartial和Html.RenderAction
查看>>
JS常用方法总结,及jquery异步调用后台方法实例
查看>>
JQuery获取input checkbox
查看>>
【PHP】Windows下配置用mail()发送邮件
查看>>
dig的用法
查看>>
组合数之和2
查看>>
Dubbo#编译动态扩展类
查看>>
ios 图片转换大小
查看>>
eclipse取消js验证
查看>>
OpenGL 你好,三角形 练习一 二 三
查看>>
appium常用api
查看>>
函数的调用过程(栈帧)
查看>>
QUARTZ CRON表达式
查看>>
suite integration toolkit executable 已停止工作
查看>>
sed- 文本流编辑器
查看>>