Search code examples
androidpythonsocketstcpdumpconnection-refused

Connection refused from android tcp client to python tcp server


I have a problem implementing socket connection between an android client (on physical device) and python server (on my pc). When the client tries to connect to the server, I get an error of ConnectionRefused. But if I try to connect in localhost with telnet, it works. If I run telnet from the android device the connection is refused. I lookup the tcpdump and i find out that the client sends a request of starting connection, but the server sends back a reset request.

Here the code:

server python:

import socket

bind_ip = socket.gethostname()
bind_port = 5000

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(5)

print("[*] Listening on %s:%d" % (bind_ip, bind_port))

while True:
    client, addr = server.accept()
    print("[*] Accepted connection from: %s:%d" %  (addr[0], addr[1]))

android client activity:

public class Test2Activity extends BaseActivity implements View.OnClickListener{

    Button mButton;
    EditText mStringEdit;
    EditText mIpEdit;

    final int port = 5000;

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

        mButton = findViewById(R.id.send_button);
        mStringEdit = findViewById(R.id.string_edit_text);
        mIpEdit = findViewById(R.id.ip_edit_text);
        mButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        sendData();
    }

    private void sendData() {
        if (mIpEdit.getText().toString().equals("")) {
            Toast.makeText(this, "Server ip not setted yet!", Toast.LENGTH_SHORT).show();
        }
        if (!mStringEdit.getText().toString().equals("")) {
            if    (Client.getInstance().sendText(mStringEdit.getText().toString()) == 0) {
            Toast.makeText(this, "Somethings goes wrong!", Toast.LENGTH_SHORT).show();
            }
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InetAddress address = InetAddress.getByName(mIpEdit.getText().toString());
                    Socket socket = new Socket(address, port);
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

and finally tcpdump info:

12:20:41.903480 ARP, Request who-has robinho-laptop tell android-c49064c93c3f654e.station, length 28
12:20:41.903508 ARP, Reply robinho-laptop is-at 7c:5c:f8:7f:3e:c4 (oui Unknown), length 28
12:20:41.911749 IP android-c49064c93c3f654e.station.41439 > robinho-laptop.5000: Flags [S], seq 3327241168, win 14600, options [mss 1460,sackOK,TS val 40845430 ecr 0,nop,wscale 6], length 0
12:20:41.911791 IP robinho-laptop.5000 > android-c49064c93c3f654e.station.41439: Flags [R.], seq 0, ack 3327241169, win 0, length 0

Here android logcat:

E/DataScheduler: isDataSchedulerEnabled():false
W/System.err: java.net.ConnectException: failed to connect to localhost/::1 (port 50000): connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:130)
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
W/System.err:     at java.net.Socket.startupSocket(Socket.java:566)
W/System.err:     at java.net.Socket.<init>(Socket.java:226)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at java.net.Socket.<init>(Native Method)
W/System.err:     at utils.connection.Client$1.run(Client.java:42)
W/System.err:     at java.lang.Thread.run(Thread.java:841)
W/System.err: Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.Posix.connect(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at libcore.io.BlockGuardOs.connect(Native Method)
W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:176)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at libcore.io.IoBridge.connectErrno(Native Method)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:128)
W/System.err:   ... 9 more
W/System.err: java.net.ConnectException: failed to connect to laptop.station/192.168.1.6 (port 5000): connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:130)
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
W/System.err:     at java.net.Socket.startupSocket(Socket.java:566)
W/System.err:     at java.net.Socket.<init>(Socket.java:226)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at java.net.Socket.<init>(Native Method)
W/System.err:     at ui.activities.Test2Activity$1.run(Test2Activity.java:62)
W/System.err:     at java.lang.Thread.run(Thread.java:841)
W/System.err: Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
W/System.err:     at libcore.io.Posix.connect(Native Method)
W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at libcore.io.BlockGuardOs.connect(Native Method)
W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:176)
W/System.err:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
W/System.err:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:631)
W/System.err:     at libcore.io.IoBridge.connectErrno(Native Method)
W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:128)
W/System.err:   ... 9 more

Anyone can explain me this issue ?


Solution

  • You probably bind your socket to a wrong ip.

    From https://pymotw.com/2/socket/tcp.html

    Many servers have more than one network interface, and therefore more than one IP address. Rather than running separate copies of a service bound to each IP address, use the special address INADDR_ANY to listen on all addresses at the same time. Although socket defines a constant for INADDR_ANY, it is an integer value and must be converted to a dotted-notation string address before it can be passed to bind(). As a shortcut, use the empty string '' instead of doing the conversion.

    server_address = ('', 5000)
    sock.bind(server_address)