function testSocket() {
switch(plus.os.name) {
case "Android":
// Android平台: plus.android.*
SocketChannel = plus.android.importClass("java.nio.channels.SocketChannel");
Selector = plus.android.importClass("java.nio.channels.Selector");
SelectionKey = plus.android.importClass("java.nio.channels.SelectionKey");
InetSocketAddress = plus.android.importClass("java.net.InetSocketAddress");
SocketAddress = plus.android.importClass("java.net.SocketAddress");
ByteBuffer = plus.android.importClass("java.nio.ByteBuffer");
Iterator = plus.android.importClass("java.util.Iterator");
// var iOException = plus.android.importClass("java.io.IOException");
var method = plus.android.importClass("java.lang.reflect.Method");
var OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");
//BufferedReader = plus.android.importClass("java.io.BufferedReader");
//var charset = plus.android.importClass("java.nio.charset");
//测试改良
var StrictMode = plus.android.importClass("android.os.StrictMode");
var Build = plus.android.importClass("android.os.Build");
if(Build.VERSION.SDK_INT > 9) {
var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
sendbuffer = ByteBuffer.allocate(100);
receivebuffer = ByteBuffer.allocate(100);
//v selectionKeys;
//iterator=new Iterator();
selectionKey = new SelectionKey();
client = new SocketChannel();
btyeSend = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6];
count1 = 0;
testThread();
break;
case "iOS":
// iOS平台: plus.ios.*
break;
default:
// 其它平台
break;
}
}
function testThread() {
var App = plus.android.runtimeMainActivity();
var Thread = plus.ios.importClass("java.lang.Thread");
socketChannel = SocketChannel.open();
//ByteBuffer dst;
socketChannel.configureBlocking(false);
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_CONNECT | SelectionKey.OP_WRITE);
//socketAddress = new SocketAddress();
socketAddress = new InetSocketAddress("10.131.164.32", 1991);
socketChannel.connect(socketAddress);
var llflag = 0;
var Runnable = plus.android.implements("java.lang.Runnable", {
"run": function() {
//选择一组键,其相应的通道已为 I/O 操作准备就绪。
//此方法执行处于阻塞模式的选择操作。
//返回此选择器的已选择键集。
while(true) {
selector.select();
//System.out.println(selectionKeys.size());
selectionKeys = selector.selectedKeys();
plus.android.importClass(selectionKeys);
//selectionKeys.clear();
iterator = selectionKeys.iterator();
plus.android.importClass(iterator);
while(iterator.hasNext()) {
selectionKey = iterator.next();
if(selectionKey.isConnectable() && (llflag == 0)) {
console.log("client connect");
llflag = 1;
client = selectionKey.channel();
// 判断此通道上是否正在进行连接操作。
// 完成套接字通道的连接过程。
if(client.isConnectionPending()) {
client.finishConnect();
console.log("完成连接!");
sendbuffer.clear();
// BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File("D:\\BigData.zip")));
// byte[] b = new byte[2];
// br.read(b);
//b[0] = 53;
//b[1] = 54;
// b =[1,2,3];
sendbuffer.put(btyeSend);
sendbuffer.flip();
// System.out.println(System.currentTimeMillis());
client.write(sendbuffer);
//ystem.out.println(System.currentTimeMillis());
}
// client.register(selector, SelectionKey.OP_READ);
} else if(selectionKey.isReadable()) {
client = selectionKey.channel();
//将缓冲区清空以备下次读取
receivebuffer.clear();
//读取服务器发送来的数据到缓冲区中
count = client.read(receivebuffer);
if(count > 0) {
receiveText = new String(receivebuffer.array(), 0, count);
console.log("客户端接受服务器端数据--:" + receiveText);
// if (client.isConnectionPending()) {
client.finishConnect();
console.log("完成连接!");
sendbuffer.clear();
// BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File("D:\\BigData.zip")));
var b;
b = receivebuffer.array();
var a = []
for(var i = 0; i < count; i++)
a[i] = b[i];
sendbuffer.put(a);
sendbuffer.flip();
// System.out.println(System.currentTimeMillis());
client.write(sendbuffer);
//ystem.out.println(System.currentTimeMillis());
// }
}
}
if(sendFlag == 1) {
client.finishConnect();
sendbuffer.clear();
//byte[] b = new byte[5];
// br.read(b);
var b_buff = [];
for(var i = 0; i < count1; i++) {
b_buff[i] = btyeSend[i];
}
sendbuffer.put(b_buff);
sendbuffer.flip();
client.write(sendbuffer);
sendFlag = 0;
}
}
//selectionKeys.clear();
}
}
});
//方式1启动线程
App.runOnUiThread(Runnable);
}
function outValue() {
for(var i = 0; i < 16; i++)
btyeSend[i] = 0;
btyeSend[0] = 65;
btyeSend[1] = 66;
count1 = 2;
sendFlag = 1;
}
w***@gmail.com
- 发布:2017-05-26 13:01
- 更新:2019-09-24 16:42
- 阅读:3505
android非阻塞socket
分类:Native.js
5 个评论
要回复文章请先登录或注册
h***@163.com
y***@qq.com
FYC
n***@qq.com
2***@qq.com