Search code examples
javaandroidsocketstcpclient

TCP socket connection refused on Android


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>

Solution

  • 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