Android端UDP1
Android模拟器UDP通信界面布局如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送的内容:"/> <EditText android:id="@+id/sendText" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/sendBtn" android:text="发送"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="接收到的内容:" android:textSize="20sp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/receiveText" android:textSize="20sp" android:text="无"/> </LinearLayout> </LinearLayout>
package com.njyd.internettest2; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; public class MainActivity extends AppCompatActivity { private boolean isText = false; final int MSG = 0x11; private Button btn; private EditText et; private TextView tv; private String st; private Message message; private Handler handler = new Handler() { //通过Handler处理UDP接收消息后对UI进行的操作 @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == MSG) { tv.setText(st); } } }; DatagramSocket socket; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initLisetener(); try { receiveMsg(); } catch (Exception e) { e.printStackTrace(); } } private void initLisetener() { btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { public void run() { //通过线程处理发送消息 String str = et.getText().toString(); Log.v("发送", str); sendMsg(str); } }).start(); } }); } private void initView() { btn = (Button) this.findViewById(R.id.sendBtn); et = (EditText) this.findViewById(R.id.sendText); tv = (TextView) this.findViewById(R.id.receiveText); } /** * 发送消息的函数 */ public void sendMsg(String msg) { Log.v("v", "开始发送"); try { if (socket == null) { // 58718是本机的端口号 socket = new DatagramSocket(58718); } //将字符串转换成字节流 byte data[] = msg.getBytes(); // UDP1的IP和端口号 DatagramPacket pack = new DatagramPacket(data, data.length, InetAddress.getByName("10.136.132.33"), 9999); // 创建DatagramPacket 对象数据包,这里的6024是我们要发送信息主机的端口号 socket.send(pack); Log.v("f", "发送成功!"); } catch (Exception e) { Log.v("f", "发送失败!"); e.printStackTrace(); } } /** * 接收消息的函数 */ public void receiveMsg() throws Exception { // 创建线程接收信息也要放在线程里面接收 new Thread(new Runnable() { public void run() { DatagramPacket pack = null; try { if (socket == null) { socket = new DatagramSocket(58718); } while (true) {//receive方法可能会阻塞,直到收到数据包 // 创建一个空的字节数组,用来接收信息 byte[] data = new byte[1024]; // 将字节数组和数组的长度传进DatagramPacket 创建的对象里面 DatagramPacket pack2 = new DatagramPacket(data, data.length); Log.v("s", "pack2"); Log.v("s", "开始 接收"); try { // socket对象接收pack包,程序启动的时候,socket会一直处于阻塞状态,直到有信息传输进来 socket.receive(pack2); } catch (IOException e) { e.printStackTrace(); } st = new String(pack2.getData(), 0, pack2.getLength()); if (isText) { message = handler.obtainMessage(MSG); handler.sendMessage(message); } } } catch (SocketException e) { e.printStackTrace(); } } }).start(); } @Override protected void onStart() { super.onStart(); isText = true; } @Override protected void onStop() { super.onStop(); isText = false; } @Override protected void onDestroy() { super.onDestroy(); // 释放资源 if (socket != null) { socket.close(); } } }
Netassist
打开UDP连接,与android端通信,运行效果如下。