茄子 快牙 什么闪传 的零流量分享原理解析

     因为要做零流量分享 ,所有首先你得了解其原理。 一台手机 开启了热点   另一台手机一扫对方二维码 就可以下载对方手机上的应用了。  不需要你得手机有 移动网络,前提是

你连上对方手机开启的热点。


      现在 说说大概的原理  


         安装了茄子或者快牙的手机 当进入零流量分享界面时 会开启热点 毋庸置疑。  其实在后台 : 茄子是 2999端口   快牙是 7989 端口。  

                         

                     ServerSocket   = new  ServerSocket ( 2999 );   这是茄子的 。

                     ServerSocket  =  new ServerSocket  (7989) ; 这是快牙的 。

        监听到请求怎么办?    看下面代码:


                                       System.out.println(filePath + " requested.");
  outstream.println("HTTP/1.1 200 OK");
outstream.println("MIME-version:1.0");
// outstream.println("Content_Type:text/html");
outstream.println("Content-Type:application/octet-stream");
int len = (int) file.length();
// outstream.println("Location:" + filePath);
outstream.println("Content-Length:" + len);
Log.d(TAG, "len: " + len);
outstream.println("Connection:close");
// outstream.println("Accept-Ranges:bytes");
outstream.println("");
sendPPt(outstream, filePath);
// outstream.println("");
outstream.flush();


                                        这是接到请求 后处理请求 , 把 自己的 应用发给 对方  用的是 http 协议 ,因为 对方使用的是 浏览器下载。



   

   完整代码就不上了,下面给出更详细的代码:


/**
* 模拟HTTP 响应发送给客户端

* @param mServer
* @param client
* @param filePath
* @param inline
* @return
*/
public static boolean HandleRequest(MyServerSocket mServer, Socket client,
String filePath, String inline) {


String destIP = client.getInetAddress().toString(); // 瀹㈡埛鏈篒P鍦板潃
int destport = client.getPort(); // 瀹㈡埛鏈虹鍙e彿
PrintStream outstream = null;
try {
outstream = new PrintStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "ip:" + destIP + " port:" + destport);
try {
System.out.println("Received:" + inline);
if (inline == null)
return false;
mServer.setKeyAndValue(inline);
if (IsgetRequest(inline)) { // 濡傛灉鏄疓ET璇锋眰
File file = new File(filePath);
if (file.exists()) { // 鑻ユ枃浠跺瓨鍦紝鍒欏皢鏂囦欢閫佺粰Web娴忚鍣�
outstream.print("HTTP/1.1 200 OK\r\n");
outstream.print("MIME-version:1.0\r\n");
// outstream.println("Content_Type:text/html");
// outstream.print("Accept-ranges:bytes\r\n");
outstream
.print("Content-Type:application/octet-stream\r\n");
// outstream.println("Server: Sunpache 1.0");
// outstream
// .println("Last-Modified: Mon, 11 Jan 2013 13:23:42 GMT");
long len = file.length();
outstream.print("Content-Length:" + len + "\r\n");
outstream.print("Connection:Close\r\n");
outstream
.print("Content-Disposition:attachment;filename=pengpengtang.apk\r\n");
// outstream.println("Connection" + "Keep-Alive");
Log.d(TAG, "len: " + len);
outstream.print("\r\n");
// 响应体
if (sendPPt(outstream, filePath, len)) {
Log.d(TAG, "send success");
}
outstream.flush();
return true;
} else { // 鏂囦欢涓嶅瓨鍦ㄦ椂
String notfound = "<html><head><title>Not Found</title></head><body><h1>Error 404-file not found</h1></body></html>";
outstream.print("HTTP/1.1 404 no found\r\n");
outstream.print("Content-Type:text/html\r\n");
outstream.print("Content-Length:" + (notfound.length() + 2)
+ "\r\n");
outstream.print("\r\n");
outstream.print(notfound + "\r\n");
outstream.flush();
return true;
}
} else
return false;
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值