【发布时间】:2020-12-19 14:18:17
【问题描述】:
这几天我已经发布了,但现在我在解决了那个问题后遇到了另一个问题。
描述: 开发一个用 kotlin 编写的充当服务器端的 android 应用程序和充当客户端的 Python 程序都在同一台计算机上运行,并尝试相互发送和接收消息。我正在使用文本视图来显示我从 PC(python) 接收到的消息。
问题:每当我尝试在 python 中运行客户端程序时。我在终端上得到以下输出。我没有收到服务器在连接时发送的消息
server sent something.....
b''
you are about to.....
而在服务器端,它不会从客户端接收任何内容。
我尝试过什么:我使用了端口转发,将客户端上的端口 5000 映射到模拟器上的 6000,正如有人在上一篇文章中建议的那样,它基本上解决了我的 error:61 connection denied 在客户端用 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
}
}
}
【问题讨论】:
-
both runs on same computer?你有安卓电脑吗?android server python client.
标签: java python android sockets server