您的位置:首页 > 移动开发 > Android开发

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

2015-06-04 23:14 661 查看
前几天需要写个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 也必须和服务端的端口号相同。

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

最后,我一个苦逼的大学生,希望通过写博客让自己能够更好地学习,所以如果那里写错了,说错了,希望大家能够指出,谢谢了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: