Xiaomi 6 calls CPP to establish Socket snooping, local loopback connection timed out

problem description

the framework used is Cocos2d-x, and the language is CPP.
due to requirements, the project needs to do a set of network layer in the CPP layer to take over and manage the game-related sending and receiving data. So it is written that a monitoring socket, game is connected through the local loopback address, and then the data is processed and forwarded to the target server. Some models of win32,ios and android can be run. The problem is that on some models added by Xiaomi 6 and oppo,vivo, the TCP connection to the local loop times out, and the IP and port of the telnet phone on the PC in the same LAN are not connected.

the environmental background of the problems and what methods you have tried

this problem is inevitable on Xiaomi 6. Socket monitoring is done at the beginning of the program. You can see that the monitoring is really successful through the nenstat command.

clipboard.png

PCIP pingtelnet

clipboard.png

clipboard.png

clipboard.png

127.0.0.113025connectSYN_SENT

clipboard.png

related codes

/ / Please paste the code text below (do not replace the code with pictures)

// socket
struct sockaddr_in svraddr;
svraddr.sin_family = AF_INET;
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
svraddr.sin_port = htons(port);

int opt = 1;
if (setsockopt(m_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&opt, sizeof(opt)) < 0)
{
    Log("bind error1! error:%d", opt);
    return false;
}

int on = 1;
if (setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) < 0)
{
    Log("bind error2! error:%d", on);
    return false;
}

int ret = ::bind(m_sock, (struct sockaddr*)&svraddr, sizeof(svraddr));
if (ret == SOCKET_ERROR) {
    Log("bind error3!");
    int error = -1;
    int len = sizeof(int);
    getsockopt(m_sock, SOL_SOCKET, SO_ERROR, (char*)&error, (socklen_t *)&len);
    Log("error = %d", error);
    return false;
}

ret = listen(m_sock, backlog);
if (ret == SOCKET_ERROR) {
    Log("listen error!");
    return false;
}
return true;

initial suspicion may be limited by Xiaomi"s firewall, but there is no evidence or solution.
I hope you tech bosses can give us some advice


it is possible that the firewall (iptables) rule is blocked, or the SELinux policy is blocked. Either way, it's probably hard to change its rules on users' phones.

if the socket you want to listen to is only open to native applications, it is recommended to use a non-anonymous unix socket for cross-process communication.

refer to http://man7.org/linux/man-pag.

Menu