为android写的一个UDP Socket连接组件
16lz
2021-01-23
https://github.com/SuperSanders/android-network
适用于Java Android上的Socket连接组件。
package android.net.udp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketAddress;import java.net.SocketException;public class SocketBase {private int dataLength = 1024;private DatagramSocket mSocket;private DatagramPacket receivePacket;private SocketAddress sendAddress;private SocketCallback callback;public SocketBase(SocketCallback callback) throws SocketException {byte[] receiveData = new byte[dataLength];receivePacket = new DatagramPacket(receiveData, dataLength);mSocket = new DatagramSocket();this.callback = callback;}public void receive() throws IOException {mSocket.receive(receivePacket);if (callback != null) {int len = receivePacket.getLength();byte[] temp = new byte[len];System.arraycopy(receivePacket.getData(), 0, temp, 0, len);callback.receive(temp);temp = null;}}public void setSendAddress(String host, int port) {sendAddress = new InetSocketAddress(host, port);}public void send(byte[] data) throws IOException {if (data.length < dataLength) {DatagramPacket sendPacket = new DatagramPacket(data, data.length, sendAddress);mSocket.send(sendPacket);} else {throw new IOException("发送数据包大于限定长度");}}public void close() {if (mSocket != null) {if (!mSocket.isClosed()) {mSocket.close();}if (mSocket.isConnected()) {mSocket.disconnect();}}mSocket = null;}}
package android.net.udp;import java.io.IOException;import java.net.SocketException;import java.util.Vector;public class SocketConnect implements Runnable {private static final Vector<byte[]> datas = new Vector<byte[]>();// 待发送数据队列private String host = null;private int port = -1;private boolean isConnect = false;private boolean isReceive = false;private boolean isSend = false;private SocketBase mSocket;private SendRunnable sendRunnable;private SocketCallback callback;public SocketConnect(SocketCallback callback) {this.callback = callback;sendRunnable = new SendRunnable();}@Overridepublic void run() {if (host == null || port == -1) {throw new NullPointerException("new set address");}isConnect = true;while (isConnect) {try {System.out.println("UDP正在连接");mSocket = new SocketBase(callback);} catch (SocketException se) {try {Thread.sleep(1000 * 6);continue;} catch (InterruptedException e) {continue;}}mSocket.setSendAddress(host, port);isReceive = true;isSend = true;System.out.println("UDP连接成功");new Thread(sendRunnable).start();System.out.println("开始接受UDP消息");while (isReceive) {try {mSocket.receive();} catch (IOException e) {break;}}sendRunnable.stop();mSocket.close();isReceive = false;mSocket = null;}}public void setRemoteAddress(String host, int port) {this.host = host;this.port = port;}public void send(byte[] data) {sendRunnable.send(data);}public void stop() {isConnect = false;isReceive = false;mSocket.close();sendRunnable.stop();}private class SendRunnable implements Runnable {@Overridepublic void run() {synchronized (this) {while (isSend) {if (datas.size() < 1) {try {this.wait();} catch (InterruptedException e) {continue;}}while (datas.size() > 0) {byte[] buffer = datas.remove(0);// 获取一条发送数据if (isSend) {try {mSocket.send(buffer);} catch (IOException e) {continue;}} else {this.notify();}}}}}public synchronized void stop() {isSend = false;this.notify();}public synchronized void send(byte[] data) {datas.add(data);this.notify();}}}
package android.net.udp;public abstract interface SocketCallback {public static final int UDP_DATA = 1;public abstract void receive(byte[] buffer);}
使用方法:
SocketConnect connect = new SocketConnect(new SocketCallback() {@Overridepublic void receive(byte[] buffer) {System.out.println("Server Message :" + new String(buffer)); }});connect.setRemoteAddress("localhost", 9999);new Thread(connect).start();
更多相关文章
- android sqlite3 数据库升级,加字段
- android 读取json数据(遍历JSONObject和JSONArray
- Android listView FastScroll 快速查询数据
- 柱状图以及饼图进行数据统计
- android 升级数据库 修改表结构
- android从web应用读取xml和json数据实战
- android get请求数据
- android intent 传数据
- [置顶] (柯昌合)Android Sqlite 持久化框架。类似于hibernate的s