How to make android app as server and python as client side

喜欢而已 提交于 2021-01-04 03:09:44

问题


Description

So basically i am building an android app in which i am using kotlin and which will behave as server.Whilst on the client side i am using python.I am using sockets for this purpose.I have to communicate using UDP.But i am unable to make connection to my android app.In python script sock.connect(('10.0.2.2', 6000)) i have also tried placing the emulator ip instead of localhost.I just want to send and receive simple messages.

Another Issue: Python script just times out it does not even gets into the loop.

Server.kt

package com.example.soundsource

import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.lang.Exception
import java.lang.ref.WeakReference
import java.net.ServerSocket
import java.net.Socket


class MainActivity : AppCompatActivity() {

    private lateinit var textView:TextView
    private var message = " "
    private lateinit var client:Socket

    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{
           t.start()
        }
    }
    private val t = Thread(Runnable {
        val port = 5000
        val server = ServerSocket(port)
        this.message = "Message from client"
        this@MainActivity.runOnUiThread {
          this.textView.text = server.localPort.toString()
        }
        val i =  InputStreamReader(this.client.getInputStream())
        val b =  BufferedReader(i)
        while (true){
            this.client = server.accept()
            message += " "+ this.client.localAddress.toString()+b.readLine()
            this@MainActivity.runOnUiThread{
                this.textView.text = server.localSocketAddress.toString()
            }
            t2.start()
        }
    })

    private val t2 = Thread(Runnable {
        val p = PrintWriter(this.client.getOutputStream())
        p.println("sending message back")
        runOnUiThread {
            this.textView.text = "message sent...."
        }
    })
}

Client.py

import socket


def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('10.0.2.2', 5000))
    while True:
        print("you are about to.....")
        data = sock.recv(1024)
        print('you received :', data)
        sock.sendall(bytes("hey kotlin....", 'utf-8'))


main()

来源:https://stackoverflow.com/questions/63617435/how-to-make-android-app-as-server-and-python-as-client-side

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!