Android 为什么可以';我不能通过插座连接吗
我正在制作一个安卓应用程序,它可以连接PC。我正在使用我找到的解决方案 当我试图通过提供准确的IP地址与PC连接时,一切正常。手机与PC快速连接,或者(当PC上的服务器未运行时)我得到无法快速连接的信息。以下是代码:Android 为什么可以';我不能通过插座连接吗,android,sockets,Android,Sockets,我正在制作一个安卓应用程序,它可以连接PC。我正在使用我找到的解决方案 当我试图通过提供准确的IP地址与PC连接时,一切正常。手机与PC快速连接,或者(当PC上的服务器未运行时)我得到无法快速连接的信息。以下是代码: public class ConnectPhoneTask extends AsyncTask<String,Void,Boolean> { @Override protected Boolean doInBackground(String... p
public class ConnectPhoneTask extends AsyncTask<String,Void,Boolean> {
@Override
protected Boolean doInBackground(String... params) {
boolean result = true;
try {
InetAddress serverAddr = InetAddress.getByName(params[0]);
socket = new Socket(serverAddr, Constants.SERVER_PORT);//Open socket on server IP and port
} catch (IOException e) {
Log.e("remotedroid", "Error while connecting", e);
result = false;
}
return result;
}
@Override
protected void onPostExecute(Boolean result)
{
isConnected = result;
Toast.makeText(context,isConnected?"Connected to server!":"Error while connecting",Toast.LENGTH_LONG).show();
try {
if(isConnected) {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true); //create output stream to send data to server
}
}catch (IOException e){
Log.e("remotedroid", "Error while creating OutWriter", e);
Toast.makeText(context,"Error while connecting",Toast.LENGTH_LONG).show();
}
}
}
public class DevicesListTask extends AsyncTask<String,Void,List<Device>>
{
@Override
protected void onPreExecute() {
pb.setVisibility(ProgressBar.VISIBLE);
}
@Override
protected List<Device> doInBackground(String... params) {
List<Device> devices=new ArrayList<Device>();
String device_ip;
Socket socket;
for(int i=0;i<4;i++)
{
device_ip=params[0]+Integer.toString(i);
try {
InetAddress serverAddr = InetAddress.getByName(device_ip);
socket = new Socket(serverAddr, 8988);
devices.add(new Device(device_ip,socket.getInetAddress().getHostName()));
} catch (IOException e) {
Log.e("remotedroid", "Error while connecting", e);
}
}
return devices;
}
@Override
protected void onPostExecute(List<Device> devices) {
pb.setVisibility(ProgressBar.INVISIBLE);
if(devices!=null)
{
Intent intent = new Intent(context,DevicesList.class);
String[] devicesIPS = new String[devices.size()];
String[] devicesNames = new String[devices.size()];
for(int i=0;i<devices.size();i++)
{
devicesIPS[i]=devices.get(i).getIP();
devicesNames[i]=devices.get(i).getName();
}
intent.putExtra("DEVICES_IPS",devicesIPS);
intent.putExtra("DEVICES_NAMES",devicesNames);
startActivity(intent);
}
else
{
Toast.makeText(context,"nope",Toast.LENGTH_LONG).show();
}
}
}
我在尝试连接到我的PC时得到了它,但我只有在通过地址循环时才得到这个异常,而不是在第一个示例中。在使用第一个示例中的代码时,我立即建立了连接。连接超时的第二段代码出了什么问题?您是否在应用程序的清单文件中设置了internet权限?如果没有,那么Android会阻止你。当你尝试连接到不存在的IP地址时,你会遇到连接超时。默认超时约为一分钟。如果出现连接超时,可以按如下方式缩短连接超时:
Socket socket = new Socket(); // create an unconnected socket
int timeout = 5000; // in milliseconds, tune as required
socket.connect(new InetSocketAddress(serverAddr, 8989), timeout);
在大多数情况下,5秒就足够了,你可以把它缩短到2-3秒,而不是更少。是的,我有
。当我有电脑的确切IP时,我没有任何连接问题。当我通过地址循环时,问题就开始了。问题是,当我使用第一个代码示例时,我并没有得到连接超时,就像在我链接的教程中一样。问题是当我试图检查循环中的许多地址时。为什么连接在一个实例中工作而在另一个实例中不工作?问题是连接成功时不会出现超时,但是当您循环访问IP地址时,不存在的IP地址会出现连接超时,它们需要一分钟才能过期。我已经说明了所有这些。我只在连接到我的电脑时才会超时。当不使用IP时,我会立即获得有关它的信息。这与你的问题中所说的相反,而且也没有意义。连接到不存在的IP地址时,无法获得超时。
Socket socket = new Socket(); // create an unconnected socket
int timeout = 5000; // in milliseconds, tune as required
socket.connect(new InetSocketAddress(serverAddr, 8989), timeout);