bundle开发----Socket通讯(Client)
来源:互联网 发布:在线绘制流程图软件 编辑:程序博客网 时间:2024/06/08 14:53
上篇记录了作为Server端bundle的实现,本文看看作为Client端如何实现一个bundle,代码改动的地方很小。
先来看看WifiBundle.java,代码如下(远程服务器地址为:192.168.0.1,端口号为:16384):
package demo.wifi.bundle; import java.io.IOException; import java.net.SocketAddress; import java.net.InetSocketAddress; import java.util.logging.Level; import java.util.logging.Logger; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; import java.nio.channels.ServerSocketChannel; import org.osgi.framework.BundleContext; import java.nio.channels.NotYetConnectedException; public class WifiBundle { public static BundleContext bc = null; private final static Logger logger = Logger.getLogger(WifiBundle.class.getName()); public volatile static SocketChannel socketChannel = null; public static ServerSocketChannel serverSocketChannel; public static boolean wifiRunningFlag = false; public static boolean wifiConnectionError = false; public static class WifiRunable implements Runnable { int totalSize = 0; int leftSize = 0; static String eventTopic = null; int arrayPosition = 0; boolean rtn; ByteBuffer receiveBuffer = ByteBuffer.allocateDirect(1024); public void run() { try { // Socket connection socketChannel = SocketChannel.open(); SocketAddress socketAddress = new InetSocketAddress("192.168.0.1", 16384); if(!socketChannel.connect(socketAddress)) { System.out.println("Wifi connection failed!"); } else { System.out.println("Wifi connection OK!"); } while (wifiRunningFlag) { receiveBuffer.clear(); rtn = receivePackage(receiveBuffer, 128); if(!rtn) { receiveBuffer.clear(); break; } receiveBuffer.clear(); sendPackage(receiveBuffer); receiveBuffer.clear(); } } catch (Exception ex) { logger.log(Level.SEVERE, null, ex); } finally { try { socketChannel.close(); socketChannel = null; System.out.println("socketChannle closed!\n"); } catch(Exception ex) { return; } } } } public static void sendPackage(ByteBuffer buffer) throws IOException { try { socketChannel.write(buffer); } catch(IOException ex) { ex.printStackTrace(); System.out.println("error occured in sendPackage()!"); wifiConnectionError = true; return; } return; } public static boolean receivePackage(ByteBuffer byteBuffer, int size) throws IOException { int arrayPos = 0; int tempSize ; int leftSize = size; int totalSize = size; byte[] tempByteArray = new byte[totalSize]; ByteBuffer tempBuffer = ByteBuffer.allocateDirect(1); while((leftSize > 0) && (wifiRunningFlag == true)) { try { while(((tempSize = socketChannel.read(tempBuffer)) != -1) && (wifiRunningFlag == true) ) { tempBuffer.flip(); if (tempSize == 0) { continue; } else { tempBuffer.get(tempByteArray, arrayPos, tempSize); leftSize --; arrayPos ++; if(leftSize == 0) { break; } } } } catch (IOException ex) { ex.printStackTrace(); System.out.println("error occured in receivePackage()!"); wifiConnectionError = true; return false; } } byteBuffer.put(tempByteArray, 0, size); return true; } }
再来看看Activator.java,代码比较简单,只需在start和stop函数中分别开始和终止socket收发进程。
package demo.wifi.bundle; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import demo.wifi.bundle.WifiBundle.WifiRunable; import demo.wifi.bundle.WifiBundle; public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("wifi bundle started!"); WifiEventPublisher.bc = context; WifiEventPublisher.eventRegister(); //start Wifi new thread new Thread(new WifiRunable()).start(); WifiBundle.wifiRunningFlag = true; } public void stop(BundleContext context) throws Exception { WifiBundle.wifiRunningFlag = false; WifiEventPublisher.EventUnregister(); if(WifiBundle.socketChannel.isConnected()) { WifiBundle.socketChannel.close(); } System.out.println("wifi bundle stoped!"); } }
- bundle开发----Socket通讯(Client)
- bundle开发----Socket通讯(Server)
- bundle开发----串口通讯
- C#开发的TCP client通讯界面
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- 使用C#开发Socket通讯
- iOS开发-Socket通讯方式
- Android开发--Socket通讯解析
- java socket通讯之 echo server 和echo client例子
- C#通讯编程(整理)Socket通讯
- Socket开发之通讯协议及处理
- Socket开发之通讯协议及处理
- OPENCV霍夫变换使用方法——边缘检测+霍夫变换求出图像中的直线
- PLSQL Developer 你用的很方便吗,看看就方便了
- 2012年最值得关注的10家云计算公司
- java高并发解决方案
- 各种音视频编解码学习详解(8)--Real系列
- bundle开发----Socket通讯(Client)
- LVS+keeplived+nginx+tomcat高可用、高性能jsp集群
- Android多屏幕适配
- 各种音视频编解码学习详解(9)--QuickTime系列
- 聊聊并发(三)——JAVA线程池的分析和使用
- 软文投稿的网站与软文投稿经验分享
- hdu 2114
- 各种音视频编解码学习详解(10)--Ogg系列
- 帮助文档