javaandroidsocketsclient-serverdataoutputstream

cannot get android app to connect to my server on my local computer


Hi I'm new to android and Im Trying to get my android app to connect to a my server that is running on my local computer. Im just trying to get the app to send a simple string message to the server first but keep getting an error. From the print out statements i can see that the app terminates as soon as it hits the line with the try in it so i believe there is an issue with creating the client connection. I have also enable Internet,ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE,READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE permissions

i tried changing the localhost to InetAddress.getLocalHost() but still wouldn't work

heres my code :

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.StringBuilderPrinter;
import android.view.View;
import android.widget.Button;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import static java.net.InetAddress.getLocalHost;

public class MainActivity extends AppCompatActivity{
    private Button cam;
    private Socket client;
    DataOutputStream os;
    private String IP = "localhost";
    private static final String TAG = "testing";

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

        if (android.os.Build.VERSION.SDK_INT > 9)
        {
            StrictMode.ThreadPolicy policy = new                      
            StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        setContentView(R.layout.activity_main);



        cam = (Button) findViewById(R.id.Button_camera);
        cam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Log.v(TAG, "trying to connect to server");
                try {
                    client = new Socket("My IP address entered", 8080);
                    Log.v(TAG, "client socket initalised");
                    String hey = "hey";
                    os = new DataOutputStream(client.getOutputStream());
                    Log.v(TAG, "outptu stream created");
                    os.flush();
                    Log.v(TAG, "flush");
                    os.writeBytes(hey);
                    Log.v(TAG, "written to server");
                    os.close();


                } catch (UnknownHostException e) {
                    Log.v(TAG, "Unknown error");
                    Log.v(TAG, e.toString());
                } catch (IOException e) {
                    Log.v(TAG, "IO exception u numpty");
                    Log.v(TAG, e.toString());
                }
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);



            }
        });

    }
}

my code prints out the statement trying to connect to server but doesn't reach any of the other print out statements and my server is still not receiving anything

 connect failed: ETIMEDOUT (Connection timed out)
ACTION_DOWN before UnsetPressedState. invoking mUnsetPressedState.run()
I/Choreographer: Skipped 3792 frames!  The application may be doing too much work on its main thread.

Solution

  • I believe this is definitely just an issue with your IP, you are obviously giving ti the wrong ip address because everything else is fine. I would recommend either accessing the terminal and using the ifconfig command to find your ip or access your network settings, if your using a mac like I am select network , advance options , select tcp/ip tab and use ipv4 address as the ip address. If that doesn't work try using a different port or checking your firewall setting on your local computer. Hope that helps.