Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Android app connect to local server

  • 22-10-2011 09:25PM
    #1
    Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭


    Hi I am running an android emulator in eclipse which is basically sending a string to a local java server. the server is just outputting the string(just a useless test to help me learn)

    i will try to explain this well:
    snippet of my java server running on same PC as android emulator(4.0):
    //makes a socket listening on port 4242
                ServerSocket serverSocket = new ServerSocket(4242);
                System.out.println("RUNNING");
                String location;
                
                
                while(true){
                    Socket socket = serverSocket.accept();
                    
                    InputStreamReader streamReader = new InputStreamReader(socket.getInputStream());
                    BufferedReader reader = new BufferedReader(streamReader);
                    
                    location = reader.readLine();
                    System.out.println(location);                
                }    
    
    snippet of code not executing in android app:
    @Override
            protected String doInBackground(String... locations) { 
                Log.d(TAG, "here in doBackground START");
                String result = "Not Done";
                try{
                    Log.d(TAG, "1");
                    //connect to socket(server)   
                    Socket socket = new Socket("<MY IP ADDRESS>", 4242);
                    Log.d(TAG, "2");
                    String sendLocation = locations[0];
                    Log.d(TAG, "3");
                    if(socket.getPort() == 0){Log.d(TAG, "null connect");}
                    Log.d(TAG, "4");
                    //for writing to server
                    DataOutputStream outToServer = new DataOutputStream(socket.getOutputStream());
                    Log.d(TAG, "5");
                    outToServer.writeBytes(sendLocation + '\n');
                    Log.d(TAG, "6");
                    socket.close();
                    Log.d(TAG, "7");
                    result = "Done";
                    Log.d(TAG, "8");
                }
                catch(Exception ex){
                    ex.printStackTrace();
                }
    
    ***Note:
    I have put all the Log.d(TAG, NUMBER); in as debugging info,
    it only outputs :Log.d(TAG, "1");

    android permissions:
    <uses-permission
            android:name="android.permission.INTERNET" />
    
    Any idea where i'm going wrong? i have tried connecting via my local host 10.0.2.2 and that worked fine. i doubt its a firewall problem, unless my home router is blocking it?


Advertisement