I tested Android(udp client)-Docklight(udp server)
MainActivity.java
- @Override
- switch(v.getId()){
- case R.id.connect:
- try{
- client=new UdpClient(ip,portNum);
- e.printStackTrace();
- }
- imm.hideSoftInputFromWindow(ipaddr.getWindowToken(), 0);
- client.start();
- connect.setEnabled(false);
- disconnect.setEnabled(true);
- text.setText("Connected\n");
- break;
- case R.id.disconnect:
- connect.setEnabled(true);
- disconnect.setEnabled(false);
- text.setText("Disconnected\n");
- break;
- case R.id.sendBtn:
- String message;
- message=msg.getText().toString();
- Message m=new Message();
- m.what=MSG;
- m.obj="Client : "+message;
- mHandler.sendMessage(m);
- sendingMsg(message);
- break;
- default:
- break;
- }
- }
Line 5~20 : touched connect button.
Line 10 : Creating Object(thread)
Line 16 : start thread
Line 21~26 : touched disconnect button.
Line 28~37 : touched send button.
udpclient.java (reading)
- MainActivity main;
- private int port=5000;
- public static final int BUFFER_SIZE=2048;
- public final static int DEFAULT_PORT=5000;
- private Boolean loop;
- private static final long TIME_OUT=1000;
- public static DatagramChannel channel=null;
- this.hostAddress=Addr;
- this.port=port;
- MainActivity.thread.start();
- Looper mLooper=MainActivity.thread.getLooper();
- MainActivity.mServicehandler=new ServiceHandler(mLooper);
- channel=DatagramChannel.open();
- MainActivity.selector=Selector.open();
- }
- @Override
- public void run() {
- try{
- final ByteBuffer readBuffer=ByteBuffer.allocate(BUFFER_SIZE);
- channel.configureBlocking(false);
- channel.connect(new InetSocketAddress(hostAddress, port));
- channel.register(MainActivity.selector, SelectionKey.OP_READ);
- loop=true;
- while(loop){
- Log.e("chk", "before");
- if(MainActivity.selector.select(TIME_OUT)==0)
- continue;
- Set<SelectionKey> readKeys=MainActivity.selector.selectedKeys();
- Iterator<SelectionKey> iterator=readKeys.iterator();
- Log.e("chk", "after");
- while(iterator.hasNext()){
- SelectionKey key=iterator.next();
- iterator.remove();
- if(key.isReadable()){
- readBuffer.clear();
- int length=channel.read(readBuffer);
- readBuffer.flip();
- byte[] receivedData=new byte[length];
- Message m2=new Message();
- m2.what=MainActivity.MSG;
- m2.obj="Server : "+line;
- if(m2.obj!=null)
- MainActivity.mHandler.sendMessage(m2);
- }
- }
- }
- e.printStackTrace();
- }finally{
- try{
- channel.close();
- e.printStackTrace();
- }
- }
- }
- public void quit(){
- loop=false;
- try{
- channel.close();
- }
- MainActivity.thread.quit();
- MainActivity.thread.interrupt();
- }
- }
Line 17~20 : creating HandlerThread Object and start.
Line 22~23 : open DatagramChannel and selector.
Line 34 : DatagramChannel setting NIO.
Line 35 : Connect.
Line 36 : Selector type set to Read Operation.
Line 42~71 : Reading loop.
Line 48~49 : If selector detected receive packet, transfer element.
Line 57~62 : reading.
Line 64~68 : display message.
Line 83~92 : disconnect datagramchannel
ServiceHandler.java(send)
- public final class ServiceHandler extends Handler{
- private final ByteBuffer writeBuffer;
- public ServiceHandler(Looper looper){
- super(looper);
- writeBuffer=ByteBuffer.allocate(UdpClient.BUFFER_SIZE);
- }
- @Override
- public void handleMessage(Message msg) {
- writeBuffer.clear();
- writeBuffer.put(s.getBytes());
- writeBuffer.flip();
- try{
- UdpClient.channel.write(writeBuffer);
- e.printStackTrace();
- }
- super.handleMessage(msg);
- }
- }
Line 10~23 : sending message(from sending button of Mainactivity.java )
No comments:
Post a Comment