-1

I am using a standard client code to send a string to my server. It works perfect when I am running a java project from eclipse but it doesn't work when I am running the same code via a button on my android application. After debugging I got my try code crashes in the line

Socket clientSocket = new Socket("192.168.1.91", 6789);

Please help me.

    public void button2OnClick(View k){

    //Testing Server Connection
    try{

        String sentence;

        String modifiedSentence;
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        Socket clientSocket = new Socket("192.168.1.91", 6789);
        Button button=(Button) k;
        ((Button) k).setText("Done");
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        sentence = "connection with android successful";
        outToServer.writeBytes(sentence + '\n');
        modifiedSentence = inFromServer.readLine();
        clientSocket.close();


    }
    catch(Exception IO){

    }



}
Theojim
  • 103
  • 3
  • 8
  • Maybe you need to add permission. [see this question][1] [1]: http://stackoverflow.com/questions/4074808/java-socket-ioexception-permission-denied – Surely May 01 '15 at 17:59
  • In Android, you should make internet connections in a asynctask not in the main thread. See http://developer.android.com/training/basics/network-ops/connecting.html –  May 01 '15 at 18:00
  • 1
    It doesn't crash. It throws an exception. Catch it, print it, and post it here, in your question. – user207421 May 01 '15 at 19:18
  • The key is that it crashes when run in response to a UI event, ie, on the Main thread where networking is not allowed. – Chris Stratton May 01 '15 at 19:55

1 Answers1

4

Because you do it from the main thread, you have move it to a Thread.

new Thread(new Runnable(){
    public void run(){
        //open socket
    }
}).start();

Next you have to add the internet permission on the AndroidManifest

<uses-permission android:name="android.permission.INTERNET" />

In android we can use AsyncTask witch can perform actions on main thread when the background operation is finished. It will be :

new AsyncTask<Void,Void,Void>(){

        @Override
        protected Void doInBackground(Void... params) {
            String modifiedSentence;
            BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
            Socket clientSocket = new Socket("192.168.1.91", 6789);
            DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            sentence = "connection with android successful";
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine();
            clientSocket.close();

            return null;
        }

        @Override
        protected void onPostExecute() {
            super.onPostExecute();
            Button button=(Button) k;
            ((Button) k).setText("Done");
        }
    }.execute();