[英]Sending data back and forth from android server to python client
幾天前我已經發布了這個,但現在我在解決那個問題后遇到了另一個問題。
描述:使用 android 應用程序編寫的 kotlin 作為服務器端和 Python 程序作為客戶端運行在同一台計算機上並嘗試相互發送和接收消息。我正在使用文本視圖顯示消息我從 PC(python)收到的。
問題:每當我嘗試在 python 中運行客戶端程序時,我都會在終端上跟蹤 output。我沒有收到服務器在連接時發送的消息
server sent something.....
b''
you are about to.....
而在服務器端,它不會從客戶端收到任何東西。
我已經嘗試過:我使用端口轉發將客戶端上的端口 5000 映射到模擬器上的 6000,正如有人在上一篇文章中建議的那樣,這基本上解決了我的錯誤:61 客戶端拒絕連接寫在 python 但不幸的是我遇到了這個問題。這是因為我在服務器端使用 kotlin 與 python 通信,應該改用 java。 或者我使用了錯誤的線程邏輯。
請幫幫我
import socket
def main():
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 5000))
while True:
data = client_socket.recv(1024)
print("server sent something.....\n", data)
print("you are about to.....")
client_socket.sendall(bytes("hey server....", 'utf-8'))
break
client_socket.close()
main()
package com.example.soundsource
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.TextView
import java.net.ServerSocket
import java.net.Socket
import java.io.*
class MainActivity : AppCompatActivity() {
private lateinit var textView:TextView
companion object{
const val COMMUNICATIONPORT = 6000
private lateinit var serversocket:ServerSocket
private lateinit var serverThread:Thread
private lateinit var updateConversationHandler:Handler
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// val sendButton:Button = findViewById(R.id.send_button)
val showLocation = findViewById(R.id.show_location) as? Button
showLocation?.setOnClickListener {
val intent = Intent(this,SoundLocation::class.java)
startActivity(intent)
}
textView = findViewById(R.id.text_view)
// sendButton.setOnClickListener{
// serverThread = Thread(ServerThread())
// serverThread.start()
// }
serverThread = Thread(ServerThread())
serverThread.start()
}
class ServerThread:Runnable{
override fun run() {
var socket: Socket
try {
serversocket = ServerSocket(COMMUNICATIONPORT)
} catch (e:IOException) {
e.printStackTrace()
}
while (!Thread.currentThread().isInterrupted) {
try {
socket = serversocket.accept()
val message = "client connected from ${socket.localAddress} and ${socket.localPort}....."
MainActivity().textView.text = message
val commThread = CommunicationThread(socket)
Thread(commThread).start()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
class CommunicationThread(clientSocket: Socket) : Runnable {
private var input: BufferedReader? = null
private var output:PrintWriter? = null
init {
try {
input = BufferedReader(InputStreamReader(clientSocket.getInputStream()))
output = PrintWriter(clientSocket.getOutputStream(),true)
} catch (e: IOException) {
e.printStackTrace()
}
}
override fun run() {
while (!Thread.currentThread().isInterrupted) {
try {
output!!.println("Thanks for connecting with me.....")
val read = input!!.readLine()
updateConversationHandler.post(MainActivity().UpdateUIThread(read))
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
internal inner class UpdateUIThread(private val msg: String) : Runnable {
override fun run() {
val message = "Client Says: $msg \n"
textView.text = message
}
}
}
您的 Android 應用程序正在偵聽端口 6000,而 Python 腳本正在與 5000 通信。還確定您的 Android 具有本地主機的 IP?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.