I have an extremely basic Python server and an Android client, but I can not establish the connection. I have a client also done in Python and another in Java, and both connect perfectly to the server.
I've tried different versions of Android, I've disabled antivirus and windows firewall, but it still does not work.
Here are 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()
Android Studio Code
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 very simple, I would just like to see that the server received the connection from the android client.
Thanks to anyone who can help.