Android socket通过UDP的方式发送,接收数据

来源:互联网 发布:搭配发型的软件 编辑:程序博客网 时间:2024/05/18 22:46

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();        }    }
阅读全文
0 0
原创粉丝点击