我遇到了UDP数据报的问题,因为我不能从服务器接收UDP数据包,但我可以发送它们。我看了很多例子,但都不知道我的代码出了什么问题。我终于从不同的网站找到了哪里出了问题的线索。
因此,我在这里更新了这个问题,以防它在未来可能会对某人有所帮助。下面的代码是在LG手机上的WiFi网络上运行的,构建在Android Studio4.2(2021年2月4日);SDK Platform 30;Kotlin 1.5.0上
在下面的代码部分的末尾,我写了一些关于是什么导致我的代码不能工作的注释。
这是我的MainActivity代码
//Required includes
import android.os.Bundle
import android.os.StrictMode
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.io.IOException
import java.net.*
class MainActivity : AppCompatActivity() {
//declared variables
private var clientThread: ClientThread? = null
private var thread: Thread? = null
private var tv:TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new)
//Create a thread so that the received data does not run within the main user interface
clientThread = ClientThread()
thread = Thread(clientThread)
thread!!.start()
// create a value that is linked to a button called (id) MyButton in the layout
val buttonPress = findViewById<Button>(R.id.MyButton)
tv = findViewById(R.id.rcvdData)
tv!!.text = "Data Captured"
//Create a listener that will respond if MyButton is clicked
buttonPress.setOnClickListener{
//send a UDP package as a test
sendUDP("Hello")
}
}
//************************************ Some test code to send a UDP package
fun sendUDP(messageStr: String) {
// Hack Prevent crash (sending should be done using a separate thread)
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy) //Just for testing relax the rules...
try {
//Open a port to send a UDP package
val socket = DatagramSocket()
socket.broadcast = true
val sendData = messageStr.toByteArray()
val sendPacket = DatagramPacket(sendData, sendData.size, InetAddress.getByName(SERVER_IP), SERVERPORT)
socket.send(sendPacket)
println("Packet Sent")
} catch (e: IOException) {
println(">>>>>>>>>>>>> IOException "+e.message)
}
}
//************************************* Some test code for receiving a UDP package
internal inner class ClientThread : Runnable {
private var socket: DatagramSocket? = null
private val recvBuf = ByteArray(1500)
private val packet = DatagramPacket(recvBuf, recvBuf.size)
// **********************************************************************************************
// * Open the network socket connection and start receiving a Byte Array *
// **********************************************************************************************
override fun run() {
try {
//Keep a socket open to listen to all the UDP trafic that is destined for this port
socket = DatagramSocket(CLIENTPORT)
while (true) {
//Receive a packet
socket!!.receive(packet)
//Packet received
println("Packet received from: " + packet.address.hostAddress)
val data = String(packet.data).trim { it <= ' ' }
println("Packet received; data: $data")
//Change the text on the main activity view
runOnUiThread { tv?.text = data }
}
}
catch (e1: IOException) {
println(">>>>>>>>>>>>> IOException "+e1.message)
socket?.close()
}
catch (e2: UnknownHostException) {
println(">>>>>>>>>>>>> UnknownHostException "+e2.message)
socket?.close()
}
finally{
socket?.close()
}
}
}
companion object {
val CLIENTPORT = 3000
val SERVERPORT = 3000
val SERVER_IP = "192.168.8.102"
}
}
我在清单文件中添加了以下权限
<uses-permission android:name="android.permission.INTERNET"/>
"activity_new.xml“只包含一个id为MyButton的按钮和一个id为rcvdData的TextView
gradle.build (项目)
buildscript {
ext.kotlin_version = "1.5.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle.build (模块)
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.udptry1"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
虽然我可以发送数据报UDP数据包,但我不能接收它们。在最终正确接收UDP数据包后,我只能接收单播消息(仅针对我的IP的消息),而不能接收广播消息(针对多个设备的消息)。
我不能接收消息的原因是我在我的PC上模拟接收器。Android的模拟器会改变被模拟设备的IP地址,而不会将其绑定到PC的IP地址。这意味着,虽然我的PC可以接收广播的消息,但模拟的电话不会。有关更多详细信息,请查看此链接(https://developer.android.com/studio/run/emulator-networking)
我只能接收单播而不能广播消息的原因是,一旦我的手机上的代码正常工作,我就会在编码时离开手机。这意味着手机屏幕将进入休眠状态。显然,有大量的手机在手机进入睡眠状态时为了省电而禁止收听广播信息。一旦手机被唤醒,它就会收听广播消息。
在我的手机上获取多点传送锁似乎不会影响这个功能(我只尝试了广播,所以不幸的是,我不知道如果你真的在使用多点传送套接字而不是datagramsocket的话,它是否可以工作)
发布于 2021-05-15 08:17:10
我已经找到了解决方案。代码实际上没有任何问题。我已经更新了原始问题,并对导致问题的原因发表了评论……请参阅上面的问题以获得完整的解释。
简而言之,问题在于Android的模拟器与PC的IP地址不同,其次,一旦进入休眠状态,手机就会停止收听广播的消息。
https://stackoverflow.com/questions/67503820
复制