Question

I am making an Android Application in which I need to make a connection with Local Ip address I searched a lot and found same sample code which i tried to implement in my code

http://android-er.blogspot.in/2011/01/simple-communication-using.html

http://www.pixelstech.net/article/1368328614-Android-socket-programming-example

But problem is this that i am unable to connect to IP address bearing no 10.19.21.128 And Port no 8221.

I cannot found where i am missing.If anyone has done this before or has idea or any sample which there is communication between android app and ip address then i will be thankful to him.

Note I just need to send a test msg hi to IP address.

Thanks Tushar

Was it helpful?

Solution

client

class ClientThread implements Runnable {

    @Override
    public void run() { 
        try {
            socket = new Socket();
            InetSocketAddress socketAddress = new InetSocketAddress(SERVER_IP, SERVERPORT);
            socket.connect(socketAddress);


            CommunicationThread commThread = new CommunicationThread(socket);
            new Thread(commThread).start();

        } catch (UnknownHostException e1) {

            e1.printStackTrace();
        }catch (IOException e1) {
            e1.printStackTrace();
        }
    }       
}
class CommunicationThread implements Runnable {

    private Socket clientSocket;
    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {
        this.clientSocket = clientSocket;
        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void run() {

      while (!Thread.currentThread().isInterrupted()) {


          try {
              final String read1 = input.readLine();
              updateConversationHandler.post(new updateUIThread(read1));                                                                                                    


                      } catch (IOException e) {
                            e.printStackTrace();

                        }
      }
    }
}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }
    @SuppressLint("SdCardPath")
    @Override
    public void run() {
        text1.setText(text1.getText().toString()+"Server Says: "+ msg + "\n");

        }   

}

server

class ServerThread implements Runnable {

    public void run() {
        socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                socket = serverSocket.accept();

                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();


                }catch (IOException ioException) {
                    ioException.printStackTrace();                  

                }
        }
    }
}

class CommunicationThread implements Runnable {


    private Socket clientSocket;

    private BufferedReader input;

    public CommunicationThread(Socket clientSocket) {

        this.clientSocket = clientSocket;

        try {
            this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));                          

        } catch (IOException e) {
            e.printStackTrace();
            new Thread(new ServerThread()).start();
        }
    }

    public void run() {


        while (!Thread.currentThread().isInterrupted()) {

            try {

                final String read1 = input.readLine();

                    updateConversationHandler.post(new updateUIThread(read1));                                      

            } catch (IOException e) {
                e.printStackTrace();

            }
        }
    }

}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
            text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");        
    }
}   
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top