0

The mentioned discussion in StackOverflow - wasn't helpful for me

I'm trying to make a client running on Android emulator, connect to my server running on eclipse in the same machine. The server is configured to listen on port 5000. The server doesn't appear to be the problem. When I run a Client on eclipse they can open a socket and communicate. When I try to run the Client class on Android emulator, the mainActivity tells the client to connect when it is created. but from some reason the dbugging flow reach the catch block, instead of creating the socket.

I tried using also client.connect("10.0.2.2",5000); but it didn't help.

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Client client = new Client(this);
    try {
        client.connect("192.168.1.10",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Client.java

   package com.example.oshri.p;

import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Observable;


public class Client extends Observable implements Runnable {
    MainActivity creatingActivity; // the activity that creates Client

  private Socket socket;
  private BufferedReader br;
  private PrintWriter pw;
  private boolean connected;
  private int port=5555; //default port
  private String hostName="localhost";//default host name

  public Client(MainActivity activity) {
        connected = false;
        this.creatingActivity = activity;
   }

  public void connect(String hostName, int port) throws IOException {
      if(!connected)
      {
         this.hostName = hostName;
         this.port = port;
         socket = new Socket(hostName,port);
         //get I/O from socket
         br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
         pw = new PrintWriter(socket.getOutputStream(),true);

           connected = true;
         //initiate reading from server...
         Thread t = new Thread(this);
         t.start(); //will call run method of this class
      }
  }

  public void sendMessage(String msg) throws IOException
  {
        if(connected) {
            pw.println(msg);
      } else throw new IOException("Not connected to server");
  }

  public void disconnect() {
        if(socket != null && connected)
      {
        try {
            socket.close();
        }catch(IOException ioe) {
            //unable to close, nothing to do...
        }
        finally {
            this.connected = false;
        }
      }
  }

  public void run() {
       String msg = ""; //holds the msg recieved from server
       try {
          while(connected && (msg = br.readLine())!= null)
          {
              creatingActivity.displayServerAnswer("Server:"+msg);

             this.setChanged();
                 this.notifyObservers(msg);
          }
       }
       catch(IOException ioe) { }
       finally { connected = false; }
  }

  public boolean isConnected() {
        return connected;
  }


  public int getPort(){
          return port;
      }

  public void setPort(int port){
          this.port = port;
      }

  public String getHostName(){
          return hostName;
      }

  public void setHostName(String hostName){
          this.hostName = hostName;
      }
}
Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61

1 Answers1

0

@greenapps pointed out the problem: The socket was created in the main activity.

@Squonk pointed the problem that, the IP should be 10.0.2.2 - the translation address used in an emulator to connect to the localhost of the machine the emulator is running on

the code was edited:

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ClientThread clientThread = new ClientThread(this);
    try {
        clientThread.startClientThread("10.0.2.2",5000);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

ClientThread.java

package com.example.oshri.parkit;

import java.net.Socket;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Observable;


public class ClientThread extends Observable implements Runnable {
  MainActivity creatingActivity; // the activity that creates ClientThread
  private Socket socket; //Uses to connect to the server
  private BufferedReader br; //For reading input from server.
  private PrintWriter pw; //For writing output to server.
  private boolean connected; //Status of client.
  private int port=5555; //default port
  private String hostName="localhost";//default host name

  public ClientThread(MainActivity activity) {
        connected = false;
        this.creatingActivity = activity;
   }

  public void startClientThread(String hostName, int port) throws IOException {
      if(!connected)
      {
         this.hostName = hostName;
         this.port = port;

         Thread t = new Thread(this);
         t.start(); //will call run method of this class, that first thing will create a client socket(cannot create socket in main thread)
      }
  }

  public void sendMessage(String msg) throws IOException
  {
        if(connected) {
            pw.println(msg);
      } else throw new IOException("Not connected to server");
  }

  public void disconnect() {
        if(socket != null && connected)
      {
        try {
            socket.close();
        }catch(IOException ioe) {
            //unable to close, nothing to do...
        }
        finally {
            this.connected = false;
        }
      }
  }

  private void connect(){
      // create socket to connect the server
      try {
          socket = new Socket(hostName, port);
      }
      catch(SocketException e){
          System.out.println("client socket could not be created");
      }
      catch (UnknownHostException e){
          System.out.println("UnknownHostException");
      }
      catch (IOException e){
          System.out.println("IOException");
      }

      // create buffers for socket IO
      try{
          br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          pw = new PrintWriter(socket.getOutputStream(),true);
      }
      catch (IOException e){
          System.out.println("socket buffers could not be created");
      }

  connected = true;
  }
  public void run() {
       connect(); //connects the server

   String msg = ""; //holds the msg recieved from server
   try {
      while(connected && (msg = br.readLine())!= null)
      {
         //System.out.println("Server:"+msg);
          creatingActivity.displayServerAnswer("Server:"+msg);

         //notify observers//
         this.setChanged();
        //notify+send out recieved msg to Observers
             this.notifyObservers(msg);
      }
   }
   catch(IOException ioe) { }
   finally { connected = false; }
  }
  public boolean isConnected() {
        return connected;
  }


  public int getPort(){
          return port;
      }

  public void setPort(int port){
          this.port = port;
      }

  public String getHostName(){
          return hostName;
      }

  public void setHostName(String hostName){
          this.hostName = hostName;
      }
}
Day_Dreamer
  • 3,311
  • 7
  • 34
  • 61