package net.testSocket;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class TestSocket extends Activity {
private TextView text1;
private Button but1;
private EditText edit1,Edip,Edport;
private final String DEBUG_TAG="mySocketAct";
Socket socket=null;
static String content ="";
private Selector selector = null;
private Charset charset = Charset.forName("GBK");
private SocketChannel sc = null;
private Handler handler;
@SuppressLint("HandlerLeak")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text1=(TextView)findViewById(R.id.text1);
but1=(Button)findViewById(R.id.but1);
edit1=(EditText)findViewById(R.id.edit1);
Edip=(EditText)findViewById(R.id.ip);
Edport=(EditText)findViewById(R.id.port);
but1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
String mesg=edit1.getText().toString()+"\r\n";
edit1.setText("");
Log.e("dddd", "sent id");
try {
sendData(mesg);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e)
{
Log.e(DEBUG_TAG,e.toString());
}
new Thread(new Runnable(){
public void run()
{
try
{
while (selector.select() > 0)
{
for (SelectionKey sk : selector.selectedKeys())
{
selector.selectedKeys().remove(sk);
if (sk.isReadable())
{
content ="";
SocketChannel sc = (SocketChannel)sk.channel();
ByteBuffer buff = ByteBuffer.allocate(1024);
while(sc.read(buff) > 0)
{
sc.read(buff);
buff.flip();
content += charset.decode(buff);
}
Message m=handler.obtainMessage();
m.what=0x11;
handler.sendMessage(m);
sk.interestOps(SelectionKey.OP_READ);
}
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}).start();
}
});
handler=new Handler(){
@Override
public void handleMessage(Message msg){
if(msg.what==0x11){
text1.setText(content);
}
super.handleMessage(msg);
}
};
}
public void sendData(String string)throws IOException
{
selector = Selector.open();
String ip=Edip.getText().toString();
String port=Edport.getText().toString();
int pt=Integer.parseInt(port);
InetSocketAddress isa = new InetSocketAddress(ip, pt);
sc = SocketChannel.open(isa);
sc.configureBlocking(false);
sc.register(selector, SelectionKey.OP_READ);
sc.write(charset.encode(string));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
socket.zip (38个子文件)
socket
testSocket
bin
res
classes
net
testSocket
BuildConfig.class 337B
R$layout.class 382B
R$id.class 482B
R$drawable.class 443B
TestSocket$1$1.class 3KB
R$attr.class 328B
TestSocket$2.class 976B
TestSocket$1.class 2KB
R.class 503B
R$string.class 415B
TestSocket.class 4KB
AndroidManifest.xml 740B
res
drawable-ldpi
bg1.png 30KB
logo.png 4KB
drawable-hdpi
icon.png 4KB
drawable-xhdpi
values
strings.xml 174B
drawable-mdpi
icon.png 3KB
layout
main.xml 1KB
assets
gen
net
testSocket
BuildConfig.java 156B
R.java 1KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 109B
org.eclipse.core.runtime.prefs 52B
org.eclipse.jdt.core.prefs 177B
src
net
testSocket
TestSocket.java 4KB
.project 846B
.classpath 475B
project.properties 563B
AndroidManifest.xml 740B
android_server
.settings
org.eclipse.jdt.core.prefs 330B
src
server
AndroidServer.java 1KB
.project 1KB
WebRoot
META-INF
MANIFEST.MF 36B
WEB-INF
web.xml 371B
classes
server
AndroidServer.class 2KB
lib
index.jsp 834B
.mymetadata 315B
.classpath 496B
.myeclipse
共 38 条
- 1
资源评论
南宫逸洛的爸爸
- 粉丝: 1
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功