Alessio Trecani Alessio Trecani - 27 days ago 7
Android Question

Android App can't connect with Java Server via Socket

I m trying to create a simple test application that connect via Socket to my computer (in localhost).But it thows some exception and I can't figure out how to solve it. NOTE: I m running the apk in my phone (not in an emulator)

Java Server Code

public class Main {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Thread t = new Thread(){

@Override
public void run(){
System.out.println("Server is running and listening ... ");
try{
ServerSocket ss = new ServerSocket(7000);
while(true){
Socket s = ss.accept();
System.out.println("Connesso");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println("Received from Client: "+ dis.readUTF());
dis.close();
s.close();
}
}catch(IOException e){
e.printStackTrace();
}


}
};
t.start();
}
}


And this is the
Andorid Client Code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button sendBTN;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBTN=(Button)findViewById(R.id.button);
sendBTN.setOnClickListener(this);
}

@Override
public void onClick(View v) {
Thread t = new Thread(){
@Override
public void run(){
try {
System.out.println("Starting Connection");
Socket s = new Socket("127.0.0.1", 7000);
System.out.println("Connection DONE");
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF("Let's Test The Socket");
dos.flush();
dos.close();
s.close();
System.out.println("Closing socket");
} catch (UnknownHostException e){
System.out.println("There was an Unknown Erorr:");
e.printStackTrace();
} catch (IOException e) {
System.out.println("There was an IOException:");
e.printStackTrace();
}
}
};
t.start();
Toast.makeText(this, "Messagge Sent...", Toast.LENGTH_SHORT).show();
}
}


What I get it this error:
enter image description here

I also tried some other ports like 1432 or 8000 or 8080 but the result is the same
Then I tried to change the IP from 127.0.0.1 to my own PC ip.. and what I get is this error..

enter image description here

EDIT:
I tried to run the app inside an Emulator using 10.0.2.2 as IP and everything woks fine.. I also tried to use my Private Ip in another JAVA Client program and it works fine.. So the problem is just the connection beetween my real phone and my PC (even if they are in the same network)

Answer

Make sure the IP when set to use your local machine from the emulator is 10.0.2.2

When you are using your phone and your PC:

  • If you are on the same network, make sure you're using the appropriate IP for your PC on your network as the server connection host. I usually set my physical machines to static IPs on my network (through my router) so I don't have to constantly look at what they are, but this is by no means a requirement.

  • If you are using your phone off of your home network, you will have to use the IP your ISP gives to connect, and make sure that the port is forwarded appropriately in your router if you have one set up.

In either case, you'll need to make sure the firewall is allowing incoming connections on the port you are specifying.