Android 手机间的文件传送(socket手机做服务端和客户端进行)

本文介绍了如何使用socket在Android设备间进行文件传输。通过创建一个服务端应用获取WiFi IP地址,并接收文件,另一端作为客户端选择文件并通过socket连接发送。两设备需在同一局域网内,实现类似文件传输软件的功能。

前几天需要写个android局域网通讯,首先就是想到的就是socket通讯了,不说了开始吧。
在这里,由于我们需要的是在局域网的通讯,所以我们先得写一个socket客户端吧。所以我就以一个app作为服务端一个作为客户端。

服务端
我们在以手机建立服务端,但是为了让其他手机可以连入该服务端进行通讯,我们必须获得该手机的WiFi的 ip 地址吧(不要说还不知道socket通讯的基础哟)。

 /**
         * 得到wifi连接的IP地址
         *
         * @param context
         * @return
         */
        public String getWifiIP(Context context) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            int ipAddr = wifiInfo.getIpAddress();
            String ipStr = int2string(ipAddr);
            return ipStr;
        }

这里我们必须要把得到的ip地址给公布出去,这样其他人才能连接呀。所以我们就把它显示在一个 TextView 吧。

然后我们就开始建立我们的服务端吧,这里我们新起一个线程来建立ServerSocket:

  public class SendThread extends Thread {
        @Override
        public void run() {
            super.run();
            try {
                mServerSocket = new ServerSocket(6789); 
                while (true) {
                    Socket client = mServerSocket.accept();
                    receiveFile(client);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

注意这里有一个 receiveFile(client),对,这就是当客户端把文件发过来时接受文件的函数。


    public void receiveFile(Socket socket) throws IOException {
        byte[] inputByte = null;
        int length = 0;
        try {
           din = new DataInputStream(socket.getInputStream());
            title = din.readUTF();
            //把接收到文件保存在 sdcard 根文件下
            fout = new FileOutputStream(new File("/sdcard/" + title));
            title = "/sdcard/" + title;
            inputByte = new byte[1024];
            //handler回调主函数--用于显示 “正在下载”
            Message msg1 = new Message();
            msg1.what = 1;
            handler.sendMessage(msg1);

            while (true) {
              //获取传送过来的数据
                if (din != null) {
                    length = din.read(inputByte, 0, inputByte.length);
                }
                if (length == -1) {
                    break;
                }
                System.out.println(length);
                fout.write(inputByte, 0, length);
                fout.flush();
            }
            //handler回调主函数--用于显示 “下载完成” 
            Message msg2 = new Message();
            msg2.what = 2;
            handler.sendMessage(msg2);
            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (fout != null)
                fout.close();
            if (din != null)
                din.close();
            if (socket != null) ;
            socket.close();
        }
    }

在receiveFile函数中,我们接受了数据并保存在 sdcard 中。

这样我们的服务端就大致结束了。

** 客户端**
在客户端,我们重要的就是选着文件,然后建立Socket进行通讯。
那么我们就看看Socket吧。

public class Socket_net_send extends Thread {
        @Override
        public void run() {
            super.run();
            File fi = new File(filepath);
            
            Socket socket = null;
            try {
                socket = new Socket(ipString, 6789);
          dout = new DataOutputStream(socket.getOutputStream());
                fin = new FileInputStream(fi);
                sendByte = new byte[1024];
                dout.writeUTF(fi.getName());

                while ((length = fin.read(sendByte, 0, sendByte.length)) > 0) {
                    dout.write(sendByte, 0, length);
                    dout.flush();
                }


            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (dout != null)
                    try {
                        dout.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (fin != null)
                    try {
                        fin.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (socket != null)
                    try {
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    }

在这个线程中,我选着了文件(没有贴代码),获得了 filepath 然后就建立socket了,这里注意,ipString 就是从服务端获取的ip地址,而后面的 6789 也必须和服务端的端口号相同。

然后我们就基本大功告成了,对了,还得提醒下,这两个手机必须连入同一个局域网内,或者一个手机开热点,一个手机连入。(是不是和许多手机传送软件有些 类似呀)。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值