Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android app connect to local server

Options
  • 22-10-2011 9:25pm
    #1
    Registered Users 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