Android socket通过UDP的方式发送,接收数据
2017-10-31 17:10
585 查看
Android socket通过UDP的方式来发送和接收数据,从而进行手机间的通信。
发送方:
public class SendToAIUIUtils { private static InetAddress mAddress; private static DatagramSocket socket = null; private static String ip = "255.255.255.255"; //发送给整个局域网 private static final int SendPort = 9999; //发送方和接收方需要端口一致 public static void sendContextToAIUI(final Context context, final String content) { //初始化socket try { socket = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } try { mAddress = InetAddress.getByName(ip); } catch (UnknownHostException e) { e.printStackTrace(); } //创建线程发送信息 new Thread() { private byte[] sendBuf; public void run() { try { sendBuf = content.getBytes("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } DatagramPacket recvPacket1 = new DatagramPacket(sendBuf, sendBuf.length, mAddress, SendPort); try { socket.send(recvPacket1); socket.close(); Log.e("zziafyc", "已将内容发送给了AIUI端内容为:" + content); } catch (IOException e) { e.printStackTrace(); } } }.start(); } }
接收端:
public class ReceiveUtils { private DatagramSocket socket; private static final int PhonePort = 9999;//手机端口号 private DatagramPacket packet; private volatile boolean stopReceiver; String filePath = "/sdcard/AIUI/devices.txt"; private void receiveMessage() { new Thread() { public void run() { try { socket = new DatagramSocket(PhonePort); } catch (SocketException e) { e.printStackTrace(); } byte[] receBuf = new byte[1024]; packet = new DatagramPacket(receBuf, receBuf.length); while (!stopReceiver) { try { socket.receive(packet); String receive = new String(packet.getData(), 0, packet.getLength(), "utf-8"); Log.e("zziafyc", "收到的内容为:" + receive); if (receive.contains("account") || receive.contains("gateWayId")) { saveSharePreference(receive); } else { saveToFile(receive, filePath); } } catch (IOException e) { e.printStackTrace(); } } } }.start(); } } public void saveSharePreference(String receive) { JSONObject jsonObject = null; try { jsonObject = new JSONObject(receive); if (receive.contains("account")) { SharePreferenceUtils.put(this, "account", jsonObject.getString("account")); SharePreferenceUtils.put(this, "password", jsonObject.getString("password")); } else { SharePreferenceUtils.put(this, "gateWayId", jsonObject.getString("gateWayId")); SharePreferenceUtils.put(this, "gateWayName", jsonObject.getString("gateWayName")); } } catch (JSONException e) { e.printStackTrace(); } } public void saveToFile(String str, String filePath) { try { File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } FileOutputStream outStream = new FileOutputStream(file); outStream.write(str.getBytes()); outStream.close(); } catch (Exception e) { e.printStackTrace(); } }
相关文章推荐
- android平台通过socket向C++服务器端发送数据和接收数据的相关问题
- android平台通过socket向C++服务器端发送数据和接收数据的相关问题
- socket通信——通过Udp传输方式,将一段文字数据发送出去
- java后台接收android客户端通过http方式发送的数据
- socket通信——通过Udp传输方式,将一段文字数据发送出去
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)android遥控器
- Android Socket 发送与接收数据问题: 发送后的数据接收到总是粘包
- java中socket发送数据接收数据(udp)
- 26 API-网络编程(网络概述,Socket通信机制,UDP协议发送和接收数据,TCP协议发送和接收数据)
- Java中通过TCP协议发送和接收数据-Socket编程
- UdpSocket如何实现通过代理接收多包数据
- DatagramSocket总是发送UDP数据后无法接收数据
- 通过异步方式发送和接收数据(tcp异步收发数据)
- Android Socket 发送与接收数据问题: 发送后的数据接收到总是粘包
- Android 通过Socket实现手机端向PC发送数据,并接受PC端返回的数据
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)
- android 使用UDP发送数据 DatagramSocket 创建对象为null
- 基于UDP协议的网络编程(使用DatagramSocket发送接收数据)
- Android Socket 发送与接收数据问题: 发送后的数据接收到总是粘包
- 使用DatagramSocket发送、接收数据(Socket之UDP套接字)