简体   繁体   English

kotlin tcp 套接字客户端不工作 python 服务器

[英]kotlin tcp socket client not working python server

I want to make an app that will connect to my python server using sockets.我想制作一个使用套接字连接到我的 python 服务器的应用程序。

When I press the connect button it does not even print the got connection on my PS, please help.当我按下连接按钮时,它甚至没有在我的 PS 上打印获得的连接,请帮忙。 Thank you谢谢

I have this basic code in kotlin:我在 kotlin 中有这个基本代码:

//Kotlin Code

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import java.io.DataOutputStream
import java.net.Socket

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.start_page)

    fun connect(v : View) {
        try{
            val soc = Socket("192.168.1.5", 1419)
            val dout = DataOutputStream(soc.getOutputStream())
            dout.writeUTF("1")
            dout.flush()
            dout.close()
            soc.close()
        }
        catch (e:Exception){
            e.printStackTrace()
        }
    }

}

The connect function is activated when clicked on a button, this is the xml code for my start screen单击按钮时会激活连接功能,这是我的开始屏幕的 xml 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    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">
    <Button
        android:id="@+id/connect"
        android:layout_width="108dp"
        android:layout_height="50dp"
        android:layout_marginBottom="127dp"
        android:layout_marginEnd="228dp"
        android:layout_marginStart="256dp"
        android:onClick="connect"
        android:text="@string/connect"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

and this is my code in python server:这是我在 python 服务器中的代码:

#Python code 

import socket

s = socket.socket()
s.bind(('0.0.0.0', 1419))
s.listen(5)

c, addr = s.accept()
print ('Got connection from', addr)
code_encoded = c.recv(1024)
code_decoded = code_encoded.decode('utf-8')
print(code_decoded)
c.close()
s.close()

I fixed it by implementing asynctask in my function and used java instead of kotlin but it should work similarly in kotlin as well.我通过在我的函数中实现 asynctask 并使用 java 而不是 kotlin 来修复它,但它在 kotlin 中也应该类似地工作。

The function is now, like this,现在的功能是这样的,

class ServerConnection extends AsyncTask<MainActivity.ConnParams, Void, Void> {

    @Override
    protected Void doInBackground(MainActivity.ConnParams... params)
    {
        String ip = params[0].ip;
        int port = params[0].port;
        String message = params[0].message;
        try
        {
            Socket socket = new Socket(ip, port);
            PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
            printWriter.write(message);
            printWriter.flush();
            printWriter.close();
            socket.close();
        } catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }
}

Although asynctask is not good for tasks where you need it to be in background for longer periods of time, at which time I would recommend using android services .尽管 asynctask 不适用于需要长时间处于后台的任务,但此时我建议使用android services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM