没有WIFi连接的Android套接字编程

时间:2020-12-31 07:37:11

I have written an app to start my server at home remotely. The app works without problems in the emulator and also on my smartphone (HTC desire, Android 2.2) when WiFi is enabled. However it does not work when WiFi is disabled.

我已经编写了一个应用程序来远程启动我的服务器。当启用WiFi时,该应用程序在模拟器和智能手机(HTC欲望,Android 2.2)上都可以正常运行。但是,禁用WiFi时它不起作用。

Before restarting I first check if it's already running. To do this I use sockets and I first connect to a dyndns address. After that I try to connect to my ip-box where I can switch on my computer by sending commands via a socket.

在重新启动之前,我首先检查它是否已经在运行。为此,我使用套接字,我首先连接到dyndns地址。之后我尝试连接到我的ip-box,我可以通过套接字发送命令来打开我的电脑。

When the connection to that socket fails I know the server is not running.

当与该套接字的连接失败时,我知道服务器没有运行。

The relevant code is:

相关代码是:

        socket = new Socket();
        socket.connect(new InetSocketAddress(serverName, port), 10000);
            status = socket.isConnected() == true;
        socket.close();

If there's an exception (SocketException) I know that the server is not running. This approach works perfectly when I have switched WiFi on. However if WiFi's not switched on then the connect always says it's ok, even if it could not establish a connection since the server is not available.

如果有异常(SocketException)我知道服务器没有运行。当我打开WiFi时,这种方法非常有效。但是,如果WiFi未打开,则连接始终表示没问题,即使由于服务器不可用而无法建立连接。

Is there a way to check if the the connection is really established, even if WiFi is disabled?

有没有办法检查连接是否真的建立,即使WiFi被禁用?

Any suggestions welcome!

欢迎任何建议!

Thanks,

谢谢,

Rudi

鲁迪

2 个解决方案

#1


2  

Try to open your socket like this :

尝试像这样打开你的套接字:

public boolean connect(String ip, int port) {
    try {
        this.clientSocket = new Socket(ip, port);
        clientSocket.setSoTimeout(timeout);
        this.outToServer = new DataOutputStream(clientSocket
                .getOutputStream());
        this.inFromServer = clientSocket.getInputStream();
        isconnected = true;
    } catch (IOException e) {
        Log.e("TCPclient", "connection failed on " + ip + ":" + port);
        isconnected = false;
        return isconnected;
    }
    Log.e("TCPclient", "connection to " + ip + " sucessfull");
    return isconnected;
}

If connection is not successful , it will generate an IOException (work when wifi enabled and no server , and when wifi is not enabled(HTC desire 2.3)).
This code is not really correct ,it's just a short version

如果连接不成功,它将生成IOException(当启用wifi并且没有服务器时工作,并且当未启用wifi时(HTC欲望2.3))。这段代码不是很正确,它只是一个简短的版本

EDIT Try to check wfi state like this (it is not practical but it should work)

编辑试着像这样检查wfi状态(这不实用,但它应该工作)

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (ni.isConnected()) {
        Toast.makeText(this,"Wifi enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "WiFi!");
      } else {
        Toast.makeText(this,"Wifi not enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "not WiFi!");
      }
    }

#2


1  

Don't forget to set the permission in manifest.xml to allow you app to open a socket.

不要忘记在manifest.xml中设置权限以允许应用程序打开套接字。

#1


2  

Try to open your socket like this :

尝试像这样打开你的套接字:

public boolean connect(String ip, int port) {
    try {
        this.clientSocket = new Socket(ip, port);
        clientSocket.setSoTimeout(timeout);
        this.outToServer = new DataOutputStream(clientSocket
                .getOutputStream());
        this.inFromServer = clientSocket.getInputStream();
        isconnected = true;
    } catch (IOException e) {
        Log.e("TCPclient", "connection failed on " + ip + ":" + port);
        isconnected = false;
        return isconnected;
    }
    Log.e("TCPclient", "connection to " + ip + " sucessfull");
    return isconnected;
}

If connection is not successful , it will generate an IOException (work when wifi enabled and no server , and when wifi is not enabled(HTC desire 2.3)).
This code is not really correct ,it's just a short version

如果连接不成功,它将生成IOException(当启用wifi并且没有服务器时工作,并且当未启用wifi时(HTC欲望2.3))。这段代码不是很正确,它只是一个简短的版本

EDIT Try to check wfi state like this (it is not practical but it should work)

编辑试着像这样检查wfi状态(这不实用,但它应该工作)

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ConnectivityManager cm = (ConnectivityManager)  getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo ni = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
      if (ni.isConnected()) {
        Toast.makeText(this,"Wifi enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "WiFi!");
      } else {
        Toast.makeText(this,"Wifi not enabled", Toast.LENGTH_LONG).show();  
        Log.d("WiFiStateTestActivity", "not WiFi!");
      }
    }

#2


1  

Don't forget to set the permission in manifest.xml to allow you app to open a socket.

不要忘记在manifest.xml中设置权限以允许应用程序打开套接字。