0

In a simple client-server TCP socket, I have a problem when I'm trying to create the client socket in Android Studio. The instruction that causes the error is Socket s=new Socket(hostname,port);

The port that I use is 1993 and I try some values for the hostname variable:

  1. hostname="127.0.0.1"
  2. hostname="localhost"
  3. hostname=""
  4. hostname=InetAddress.getByName([with-the previous 2 values])

In every case, the application stops because of this error, the error below belongs to the first point, the others get similar error.

    W/System.err: Couldn't get I/O for the connection to 127.0.0.1
    W/System.err: java.net.ConnectException: failed to connect to /127.0.0.1 (port 1993): connect failed: ECONNREFUSED (Connection refused)
            at libcore.io.IoBridge.connect(IoBridge.java:124)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
            at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
            at java.net.Socket.startupSocket(Socket.java:592)
            at java.net.Socket.tryAllAddresses(Socket.java:128)
            at java.net.Socket.(Socket.java:178)
            at java.net.Socket.(Socket.java:150)
    W/System.err:     at com.example.davide.beachapp.model.ClientSocket$MyThread.run(ClientSocket.java:92)
        Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
            at libcore.io.Posix.connect(Native Method)
            at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
            at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
            at libcore.io.IoBridge.connect(IoBridge.java:122)
            ... 7 more

The same instruction in Eclipse works, I tested it with a server "nc -l 1993" on the command-line and everything works on Eclipse.

Maybe it's some configuration problem with the manifest?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.davide.beachapp">
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>                
  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme.NoActionBar">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" >
      </intent-filter>
    </activity>
  </application>
</manifest>

2 Answers 2

3

If you are on an emulator, try to use 10.0.2.2 instead of 127.0.0.1.

For more info, visit this page:

Set up Android Emulator networking

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

2 Comments

thank you it works and there is no need to change te ip at runtime
glad to hear! happy coding :)
1

In emulator you must use the IP of your computer on the LAN. For example, in my network Gateway: 192.168.0.1 and my computer's ip is 192.168.0.2. So, in Android emulator I must make:

Socket socket = new Socket("192.168.0.2",1993);

1 Comment

what about the physical device?

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.