Android client communication with Python server

Asked

Viewed 548 times

2

I have an extremely basic server in Python and an Android client, but I can’t establish the connection. I have a client also made in Python and other in Java, and both connect perfectly to the server.

I’ve tried different versions of Android, I’ve disabled antivirus and windows firewall but still does not work.

Follow the codes:

Python code

#Programa Servidor
import socket

host = "" #IP do servidor
port = 5000 #Porta disponível do servidor

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Cria um novo socket
s.bind((host, port)) #Vincula o socket ao IP e Porta correspondente
s.listen(5) #Define o limite de conexões

print("Aguardando conexao...")

conn, addr = s.accept() #Aceita a conexão

print("Connected by", addr)

data = conn.recv(1024)

#print(data.decode())

while True:
    data = conn.recv(1024) #Aguarda uma determinada informação enviada de um remoto

    print(repr(data)) #Converte a informação para string

    if(repr(data)=="b'sair'"):
        estado = 1

conn.close()

Code of Android Studio

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="android.usuario.testesocket.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Conecta"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Main class

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button conectar = (Button) findViewById(R.id.button);

        if (conectar != null) {
            conectar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    new Thread(new ClienteThread()).start();
                }
            });
        }
    }
}

Thread class

import java.io.IOException;
import java.net.Socket;

/**
 * Created by Jefferson on 27/05/2016.
 */
public class ClienteThread implements Runnable {
    @Override
    public void run() {
        try {
            Socket socket = new Socket("192.168.0.104", 5000);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

The application is quite simple, just like to view that the server received the connection from the android client.

I appreciate anyone who can help.

  • Can you post the error? Or even trace the execution since you have some debugs along the way there.

  • The error was that network usage permission was missing on Androidmanifest.xml

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.