0
votes

all I've created simple android app which should connect to Python 3.6 server through socket and send data. It doesn't work. I've lost my mind checking what's not working. Could You help me?

Main Activity

public class Test extends AppCompatActivity  {
public Button but1;
int Ra;
public void init(){
    but1 = (Button)findViewById(R.id.button_id);
    but1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Client Connection = new Client();
          Connection.execute();


        }
    });
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET},Ra);
    init();
}}

Second file

public  class Client extends AsyncTask<Void ,Void , Void > {


@Override
protected Void doInBackground(Void... voids)
{
    final String adress = "192.168.1.3";
    final int Port = 70;
    Log.d("Connection","trying to create connection");
            try {
                Log.d("Connection", "Creating socket");
                Socket connect = new Socket(adress, Port);
                Log.d("Connection","Connected");
                DataOutputStream dout = new DataOutputStream(connect.getOutputStream());
                DataInputStream din = new DataInputStream(connect.getInputStream());
                dout.writeUTF("Hello");
                dout.flush();
                Log.d("Connection","Sent");
                dout.close();
                din.close();
                connect.close();
            }catch (IOException e){
            {
                e.printStackTrace();
                Log.d("connection",e.getMessage());
            }}
            return null;
}}

Python server

    import socket

_Connection = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#Creating socket
#binding socket to port
_ADDRESS = ('localhost', 70)
print('Starting Server on {} port {}'.format(*_ADDRESS))

_Connection.bind(_ADDRESS)

_Connection.listen(1)

while True:
    conn, addr = _Connection.accept()
    print("Connection from ", addr)
    msg = _Connection.recv(1024)
    print(msg)

Log

08-24 17:31:43.950 32614-1203/com.example.marcin.tcpiptest D/connection: failed to connect to /192.168.1.3 (port 70) from /:: (port 47696): connect failed: ETIMEDOUT (Connection timed out)

if anybody knows, what does I'm doing wrong and they would tell me, I would be grateful

1
Not working is not a good error description. Any errors? Exceptions? Crashes. Tell us what happens and not happens. - greenapps
Where does your app run? - greenapps
I would, but nothing happens. Server is not finding out any connections. - Marcin Wawrzków
I asked about the client. Which logs do you see? - greenapps
@greenapps First time my app was running on android virtual device from Android Studio, but I find out that this could be the problem. Then I've installed app on my phone which was connected to my WiFi and nothing happen again. - Marcin Wawrzków

1 Answers

-1
votes
msg = _Connection.recv(1024)

Change server to

msg = _Connection.recv(0)

Is a buffer problem, I have same. Try:

msg = _Connection.recv(10)

And send message from Android to server "1234567890server"

The server only show "server".