0
package com.example.tristan.myapplication;
import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.widget.Toast;
        import java.io.IOException;
        import java.net.Inet4Address;
        import java.net.InetAddress;
        import java.net.InetSocketAddress;
        import java.net.Socket;
        import java.net.SocketAddress;
        import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    private Socket client;
    int serverPort = 8888;
    String serverIP = "192.168.1.6";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        fctClient();
    }

    public void fctClient() {
        try {
            Toast toast = Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT);

            InetAddress addr = InetAddress.getByName(serverIP);
            SocketAddress socketAddress = new InetSocketAddress(addr,serverPort);
            //SocketAddress socketAddress = new InetSocketAddress(serverIP, serverPort);

            client = new Socket();
            toast.show();
            client.connect(socketAddress);
            toast.show();

            toast.show();`enter code here`
            client.close();

        } catch (UnknownHostException e) {
            Toast toast = Toast.makeText(getApplicationContext(), "KO1", Toast.LENGTH_LONG);
            toast.show();
            e.printStackTrace();
        } catch (IOException e) {
            Toast toast = Toast.makeText(getApplicationContext(), "KO2", Toast.LENGTH_LONG);
            toast.show();
            e.printStackTrace();
        }
    }

} Hello, I am trying to connect a client on Android on a Python server using a socket. The Python server is working well but the Android code fails to connect the server passing through a IOException. Is anyone able to tell me my mistake? The code fails on line "client.connect(socketAddress);". This let me think that the wrong command is used to generate socketAddress. I found the following link which met the same problem but his solution doesn't work for me.

(Java/Android) Client-side Socket throwing IOException

Thanking you in advance, TL

  • Have you tried reading here https://developer.android.com/studio/run/emulator-networking.html#networkaddresses ? If you are using an emulator, you could try with 10.0.2.2 which should connect the emulator to the localhost – MatPag Apr 29 '17 at 12:41
  • I am actually using a Samsung S4 mini connected to the same network as my computer (on which the Python server runs). – Tristan Lamesch Apr 29 '17 at 13:15
  • Maybe there is something related to the firewall or router or other things which block you – MatPag Apr 29 '17 at 13:54
  • Ok! So according to you my code is OK? – Tristan Lamesch Apr 29 '17 at 15:05
  • If IP and port are the right ones seems there is nothing bad in your code. In the answer you posted the user added the `client.bind(null)` before connect but i don't know if it is needed or not, sorry – MatPag Apr 29 '17 at 15:23
  • 1
    This command is required for the constructor but I agree, there are other ways! Thank you for your time – Tristan Lamesch Apr 29 '17 at 15:31

1 Answers1

0

You have a NetworkOnMainThreadExeption clearly visible in the logcat. All internet code should be executed in a thread or AsyncTask.

If you modify your code then remove all Toasts as they cannot be called in a thread or the doInBackground of an AsyncTask.

greenapps
  • 11,154
  • 2
  • 16
  • 19