Android socket通过UDP的方式来发送和接收数据,从而进行手机间的通信。
发送方:
public class SendUtils {
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 SendUtils (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);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
边发送边接收:
public class SendAndReceiveUtils {
private static InetAddress mAddress;
private static DatagramSocket socket = null;
private static String ip = "255.255.255.255"; //发送给整个局域网
private static int port = 8899; //发送方和接收方需要端口一致
private static String personIp = "127.0.0.1"; //发送给本机
private static final int personPort = 7070; //发送方和接收方需要端口一致
public static void sendSearchGateWay(Context context, final String content, final String sid, int port) {
//初始化socket
try {
socket = new DatagramSocket();
//设置超时时间
socket.setSoTimeout(5000);
} 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 (Exception e) {
e.printStackTrace();
}
DatagramPacket packet = new DatagramPacket(sendBuf, sendBuf.length, mAddress, port);
try {
//已发送完成
socket.send(packet);
byte[] receive = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receive, receive.length);
while (true) {
socket.receive(receivePacket);
String result = new String(receivePacket.getData(), 0, receivePacket.getLength(), "utf-8");
Log.e("result: ", result);
if (!TextUtils.isEmpty(result) && result.contains(sid)) {
break;
}
}
} catch (SocketTimeoutException timeOutException) {
Log.e("result4: ", "开始执行重启");
socket.close();
sendContextToAIUI_person("请先连接网关");
App.getInstance().exit();
RestartAPPTool.restartAPP(context);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}