• android ftp 服务器


    源码地址:http://ppareit.github.com/swiftp/   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方法中根据输入流中的参数判断命令类型,然后执行对应的命令

  • 相关阅读:
    局域网中CSMA/CD协议的应用
    RIP及距离向量算法
    网桥与以太网交换机
    C++ String
    C++ Input & Output
    Shell Script(1)----variable compare
    python--内建函数(1)
    python--data type
    python--compile
    python--help
  • 原文地址:https://www.cnblogs.com/lipeil/p/2613126.html
Copyright © 2020-2023  润新知