如何解决此异常java.net.ConnectException:无法从/ ::(端口49405)连接到/192.168.56.1(端口1024):

时间:2019-07-01 17:39:38

标签: java android python sockets

我正在制作一个android应用程序,通过它可以控制我的PC操作(如关机,重新启动和播放音乐)。

但这显示了异常。

2019-07-02 01:24:30.570 31431-9027 / com.sundram.myapplication W / System.err:java.net.ConnectException:无法从/:连接到/192.168.56.1(端口1024) (端口49405):连接失败:ETIMEDOUT(连接超时)

我尝试了很多关于StackOverflow的答案,但是都没有帮助。

This My Client.java

public class Client extends AsyncTask<Void, Void, Void> {

    String ip_address = "192.168.56.1";
    String type = "";

    Client(String t) {
        type = t;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        if (type == "shutDown") {
            try {
                Socket myScket = new Socket(ip_address, 1024); //getting error in this line 
                OutputStream output = myScket.getOutputStream();
                DataOutputStream opData = new DataOutputStream(output);
                opData.writeBytes("shutDown");
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else if (type == "restart") {
            try {
                Socket myScket = new Socket(ip_address, 1024);
                OutputStream output = myScket.getOutputStream();
                DataOutputStream opData = new DataOutputStream(output);
                opData.writeBytes("restart");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

}

这是我的服务器。py

from socket import *
import os
import platform

host ="192.168.56.1"
port=1024

s=socket(AF_INET,SOCK_STREAM)

s.bind((host,port))

s.listen(5)
while True:
    c,addr = s.accept()

    #print(c.recv(1024).decode('utf-8'))    

    type = c.recv(1024).decode('utf-8')

    if(type == "shutDown"):
        if(platform.system() == "Windows"):
            os.system("shutDown")
        elif(platform.system() == "Linux"):
            os.system("shutDown")
    elif(type == "restart"):
        if(platform.system() == "Windows"):
            os.system("shutDown /r")
        elif(platform.system() == "Linux"):
            os.system("shutDown -r")
    elif(type == "music"):
        if(platform.system() == "Windows"):
            os.system("K:\\1.mp3")
        elif(platform.system() == "Linux"):
            os.system("~/1.mp3")
    #s.close()

此例外

2019-07-02 01:24:30.570 31431-9027/com.sundram.myapplication W/System.err: java.net.ConnectException: failed to connect to /192.168.56.1 (port 1024) from /:: (port 49405): connect failed: ETIMEDOUT (Connection timed out)
2019-07-02 01:24:30.571 31431-9027/com.sundram.myapplication W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:138)
2019-07-02 01:24:30.572 31431-9027/com.sundram.myapplication W/System.err:     at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)
2019-07-02 01:24:30.572 31431-9027/com.sundram.myapplication W/System.err:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)
2019-07-02 01:24:30.572 31431-9027/com.sundram.myapplication W/System.err:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
2019-07-02 01:24:30.573 31431-9027/com.sundram.myapplication W/System.err:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
2019-07-02 01:24:30.574 31431-9027/com.sundram.myapplication W/System.err:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:357)
2019-07-02 01:24:30.574 31431-9027/com.sundram.myapplication W/System.err:     at java.net.Socket.connect(Socket.java:616)
2019-07-02 01:24:30.575 31431-9027/com.sundram.myapplication W/System.err:     at java.net.Socket.connect(Socket.java:565)
2019-07-02 01:24:30.575 31431-9027/com.sundram.myapplication W/System.err:     at java.net.Socket.<init>(Socket.java:445)
2019-07-02 01:24:30.576 31431-9027/com.sundram.myapplication W/System.err:     at java.net.Socket.<init>(Socket.java:217)
2019-07-02 01:24:30.576 31431-9027/com.sundram.myapplication W/System.err:     at com.sundram.myapplication.Client.doInBackground(Client.java:41)
2019-07-02 01:24:30.576 31431-9027/com.sundram.myapplication W/System.err:     at com.sundram.myapplication.Client.doInBackground(Client.java:8)
2019-07-02 01:24:30.577 31431-9027/com.sundram.myapplication W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
2019-07-02 01:24:30.577 31431-9027/com.sundram.myapplication W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2019-07-02 01:24:30.577 31431-9027/com.sundram.myapplication W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2019-07-02 01:24:30.578 31431-9027/com.sundram.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
2019-07-02 01:24:30.578 31431-9027/com.sundram.myapplication W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
2019-07-02 01:24:30.578 31431-9027/com.sundram.myapplication W/System.err:     at java.lang.Thread.run(Thread.java:764)
2019-07-02 01:24:30.580 31431-9027/com.sundram.myapplication W/System.err: Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
2019-07-02 01:24:30.580 31431-9027/com.sundram.myapplication W/System.err:     at libcore.io.Linux.connect(Native Method)
2019-07-02 01:24:30.580 31431-9027/com.sundram.myapplication W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:126)
2019-07-02 01:24:30.581 31431-9027/com.sundram.myapplication W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:152)
2019-07-02 01:24:30.581 31431-9027/com.sundram.myapplication W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:130)
2019-07-02 01:24:30.581 31431-9027/com.sundram.myapplication W/System.err:  ... 17 more

0 个答案:

没有答案