1

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)

6
  • Don't be afraid posting the LAN ip of your computer as we all use the same ip's like 192.168..... If the client can't connect using the servers ip probably a firewall is refusing connection. Commented Feb 2, 2016 at 18:48
  • But If I use the same ip in a Java Client it works fine.. How is it possible?? Commented Feb 2, 2016 at 18:59
  • You did not tell where that client runs. So how could we know what you did? Commented Feb 2, 2016 at 19:16
  • 1
    So your java client and your android client both run on your phone and use the same ip and only the java client can connect? Is it so difficult to exactly tell about your setup? Commented Feb 2, 2016 at 21:00
  • 1
    @AlessioTrecani You've been asking a lot of ill-informed, naive, questions recently, ignoring advice and actual answers you receive in favor of bruteforcing your own shoddy attempts built upon further misunderstanding. Commented Feb 3, 2016 at 17:25

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Already checked.. but I didn't solved the problem.. I will try to ask my provider to set a static IP isntead of a Dynamic one.. and I will update my thread ;)
What has this to do with your provider if phone and pc are in the same network you told and you use local ip of the pc? Nothing. You are very confusing.
I m using the same network.. firewall allow connection.. But the app doesn't work. So I can try to change this. It's just a test to solve a problem that seems to have no solution. PC and Phone in the same connection but using the right IP the app doesn't work..(I don't think this is confusing) Other people have understood the problem but nobody found the solution.
0

Ok I found the solution.

Then I deleted the exception I ve made in my firewall for port 7000 and I created a new exception which allow the connection using port 3000 and now it works fine.

7 Comments

This is not a correct solution to this problem. You do not need a static ip to allow your phone to communicate to your desktop on the same network. You don't even need a static ip address to allow it to connect from outside of the network. I've done this plenty of times without a static ip address.
Having a static ip was not the solution, your firewall probably was the issue. You stating that you had to get a static ip address to do this was superfluous and may lead others to think this is a appropriate part of the process.
Checking your firewall is what was suggested in my answer and another that appears to have been removed.
@mkorcha personally, i would've tried to be more specific about which ports to check and maybe supply some screenshots since it's obvious that OP is not very well versed with computers quite yet and needs a bit more handholding that your standard SO answer. But you're right; your answer does seem to provide the actual answer. Even though static ip's are not part of the solution, I do notice your answer mentions them. OP should accept mkorcha's answer.
@iismathwizard the reason I wasn't more specific on ports was because OP had mentioned in edits that they had tried many different ports, so I figured it would be sufficient to say to make sure the port they were using was opened. However you make a good point about static IP, I updated my answer to be less confusing about it.
|
0

If you're trying to connect the localhost listening server via Android Virtual Device, you must first check whether the "mobile data" in the AVD is in "On" state, since it doesn't work if it is in "Off" state, well I don't know the exact reason but it works like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.