ETIMEDOUT仅适用于用python编写的服务器

2024-05-20 20:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从android工作室向一些用python编写的简单服务器发送消息。奇怪的事情发生了,我成功地从eclipseide中的简单java代码发送到python服务器。在

但是当我尝试在android工作室中发送(使用相同的java代码)时,我收到了一个超时错误。在

为了测试,我编写了另一个服务器(用Java编写),并成功地从androidstudio发送到Java服务器。在

也就是说,它只会从android studio(客户端)到python(服务器)发生故障,而且没有弄清楚发生这种情况的原因。在

我的电脑地址是:192.168.1.9

android的Java客户端:(不适用于python服务器,但适用于Java服务器):

try {
            Socket client = new Socket("192.168.1.9", 5402);
            PrintWriter printwriter = new PrintWriter(client.getOutputStream(), true);
            printwriter.write("HIIIIIIII "); // write the message to output stream
            printwriter.flush();
            printwriter.close();
            boolean connection = true;
            Log.d("socket", "connected " + connection);
            // Toast in background becauase Toast cannnot be in main thread you have to create runOnuithread.
            // this is run on ui thread where dialogs and all other GUI will run.
            if (client.isConnected()) {
                Log.d("socket", "Exception");
            }
        } catch (Exception e) {
            Log.e("aaaa", e.getMessage());
        }

eclipse的Java客户端:(适用于Java服务器和python服务器):

与之前相同的代码只将地址改为“127.0.0.1”

python服务器是一个“黑匣子”,我不能更改它的代码,但它在本地主机5042端口上运行。在

我在android工作室得到的错误:

failed to connect to /192.168.1.9 (port 5402): connect failed: ETIMEDOUT (Connection timed out)

下面是一个简单的python服务器示例:

^{pr2}$

Tags: to代码服务器clientlog客户端new地址