从 android tcp 客户端到 python tcp 服务器的连接被拒绝

Connection refused from android tcp client to python tcp server

我在 android 客户端(在物理设备上)和 python 服务器(在我的电脑上)之间实现套接字连接时遇到问题。当客户端尝试连接到服务器时,出现 ConnectionRefused 错误。但是,如果我尝试在本地主机中使用 telnet 进行连接,它就可以工作。如果我从 android 设备 运行 telnet 连接被拒绝。我查找了 tcpdump,我发现客户端发送了一个开始连接的请求,但是服务器发回了一个重置​​请求。

这里是代码:

服务器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 客户 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();
    }
}

最后是 tcpdump 信息:

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

这里androidlogcat:

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.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.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

谁能给我解释一下这个问题?

您可能将套接字绑定到错误的 ip。

来自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)