简体   繁体   English

Android客户端无法打开C服务器的套接字

[英]Android client can't open a socket to a C server

I couldn't find anything about this quite specific problem, but maybe someone else has done this. 对于这个非常具体的问题,我什么也找不到,但是也许其他人已经做到了。

I have a server program written in C (error checking removed for readability): 我有一个用C编写的服务器程序(已删除错误检查以提高可读性):

int main(int arc, char *argv[])
{
    int sockfd, newsockfd, portnum, cli_len, num_chars, n;

    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;

    // Set up the socket
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    // Set up our variables
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portnum = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(portnum); // Convert from host port to network port
    serv_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
        error("Socket could not be bound");

    listen(sockfd, 5); // Get ready to receive connections

    cli_len = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &cli_len);

    // Message processing

    return 0;
}

Meanwhile, I have the Android client (Version 4.1.2): 同时,我有Android客户端(版本4.1.2):

private class ClientTask extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            try {
                InetAddress a = InetAddress.getByAddress(new byte[] {(byte) 192, (byte) 168, 1, (byte) 102});
                sock = new Socket(a, 65053);
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream()); 

                // Message processing

            } catch (UnknownHostException e) {
                System.out.println("No address found for ");
            } catch (IOException e) {
                System.out.println("Failed to open socket");
            }
            return null;
        }
    }

The issue I have is that the socket can't be opened by the Android client. 我的问题是Android客户端无法打开套接字。 I always get "Failed to open socket" after the timeout. 超时后,我总是收到“无法打开套接字”的信息。 The android client works with a java server I have, and a java client I have at least makes the connection with the C server. android客户端可以与我拥有的java服务器一起使用,而我拥有的java客户端至少可以与C服务器建立连接。 I don't understand what could be wrong... 我不明白这可能是错的...

If you need more details or additional code, let me know. 如果您需要更多详细信息或其他代码,请告诉我。

You don't specify if you're running both devices (server and client) just under the same LAN, in which case you can discard connectivity problems. 您无需指定是否在同一局域网内同时运行两个设备(服务器和客户端),在这种情况下可以丢弃连接问题。 If not, this might be a test. 如果没有,这可能是一个测试。 If you're able to connect your client to your server under the same router, that means that otherwise (ie, using your public IPs) there's something blocking it, probably your router. 如果您能够将客户端连接到同一路由器下的服务器,则意味着否则(即使用您的公共IP)存在某种阻止它的东西,可能是您的路由器。 Another issues could be antiviruses, OS ports blocking, router ports blocking... 另一个问题可能是防病毒,操作系统端口被阻止,路由器端口被阻止...

If you're not able to connect both either under the same router connection, definitely it's a code issue. 如果您无法在同一路由器连接下同时连接这两者,则绝对是代码问题。 As you don't know where the culprit is, I'd suggest putting several Log.d() lines within the client code and printf() statements within the server code in the connectivity snippets on both sides and see where the bottleneck is, or use some debugging tool and put some breakpoints. 您可能不知道罪魁祸首在哪里,我建议在客户端代码中的几条Log.d()行和服务器代码中的printf()语句放在双方的连接摘要中,并查看瓶颈在哪里,或使用一些调试工具并放置一些断点。

---- EDIT ---- ----编辑----

Try connecting this way: 尝试以这种方式连接:

Socket socket;
final String host = "192.168.1.X";
final int port = 65053;
final int timeout = 30000;   // 30 seconds

try {
  socket = new Socket();
  socket.connect(new InetSocketAddress(host, port), timeout);
}
catch (UnknownHostException uhe) {
  Log.e("Sock", "I couldn't resolve the host you've provided!");
}
catch (SocketTimeoutException ste) {
  Log.e("Sock", "After a reasonable amount of time, I'm not able to connect!");
}
catch (IOException ioe) {
  Log.e("Sock", "Hmmm... Sudden disconnection, probably you should start again!");
} 

Whelp, it was a firewall issue, embarrassingly enough. Whelp,这是一个防火墙问题,令人尴尬。 Apparently Eclipse was let through my firewall, which is why the Java server worked, but I must've missed the prompt when I ran my C code through the command line. 显然,Eclipse是通过我的防火墙的,这就是Java服务器正常工作的原因,但是当我通过命令行运行C代码时,我一定错过了提示符。 I disabled my firewall and it worked immediately. 我禁用了防火墙,它立即起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM