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

时间:2019-06-22 09:22:06

标签: java android python

我正在尝试从Android Studio向一些用python编写的简单服务器发送消息。发生了一件奇怪的事情,我成功地将Eclipse IDE中的简单Java代码发送到了python服务器。

但是当我尝试在android studio中发送(使用相同的Java代码)时,出现了超时错误。

为了进行测试,我编写了另一台服务器(用Java编写),然后我成功地从android studio发送到了Java服务器。

i.e它只能从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服务器是一个“黑匣子”,我无法更改其代码,但它在localhost的端口5042上运行。

我在android studio上遇到的错误:

  

无法连接到/192.168.1.9(端口5402):连接失败:ETIMEDOUT(连接超时)

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

import socket  # Import socket module
soc = socket.socket()         # Create a socket object
host = "localhost" # Get local machine name
port = 5402                # Reserve a port for your service.
soc.bind((host, port))       # Bind to the port
soc.listen(5)                 # Now wait for client connection.
while True:
  conn, addr = soc.accept()     # Establish connection with client.
  print ("Got connection from",addr)
  msg = conn.recv(1024)
  print (msg)

0 个答案:

没有答案