Vinay Bharadhwaj
Vinay Bharadhwaj

Reputation: 163

kotlin tcp socket client not working python server

I want to make an app that will connect to my python server using sockets.

When I press the connect button it does not even print the got connection on my PS, please help. Thank you

I have this basic code in 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 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 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()

Upvotes: 2

Views: 4802

Answers (1)

Vinay Bharadhwaj
Vinay Bharadhwaj

Reputation: 163

I fixed it by implementing asynctask in my function and used java instead of kotlin but it should work similarly in kotlin as well.

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.

Upvotes: 1

Related Questions