我有一个python套接字服务器,我的Android设备要连接到但我的Android模拟器能够连接到它但我的手机无法连接到它并给出错误ETIMEDOUT。任何人都能说出它有什么问题吗?
Python服务器:
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(10)
while True:
c, addr = s.accept()
print(addr)
c.send(bytes('Thank you for connecting','utf-8'))
c.close()
Android客户端: Client.java:
package com.example.abhishekroul.clientapp;
import android.os.AsyncTask;
import android.widget.TextView;
//import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client extends AsyncTask<Void,Void,Void>
{
String dstAddress;
int dstPort;
String response="";
TextView textResponse;
Client(String addr,int port, TextView textResponse)
{
dstAddress=addr;
dstPort=port;
this.textResponse=textResponse;
}
@Override
protected Void doInBackground(Void ...arg0)
{
Socket socket=null;
try
{
socket=new Socket(dstAddress,dstPort);
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(1024);
byte[] buffer=new byte[1024];
int bytesRead;
InputStream inputStream= socket.getInputStream();
while((bytesRead=inputStream.read(buffer))!=-1)
{
byteArrayOutputStream.write(buffer,0,bytesRead);
response += byteArrayOutputStream.toString("UTF-8");
}
}
catch(UnknownHostException e)
{
e.printStackTrace();
response="UnknownHostException:"+e.toString();
}
catch (IOException e)
{
e.printStackTrace();
response="IOException:"+e.toString();
}
return null;
}
@Override
protected void onPostExecute(Void result)
{
textResponse.setText(response);
super.onPostExecute(result);
}
}
MainActivity.java:
package com.example.abhishekroul.clientapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText address, port;
TextView response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
address = (EditText) findViewById(R.id.address);
port = (EditText) findViewById(R.id.port);
response = (TextView) findViewById(R.id.response);
}
public void funcConnect(View v)
{
Client client= new Client(address.getText().toString(),Integer.parseInt(port.getText().toString()),response);
client.execute();
}
public void funcClear(View v)
{
response.setText("");
}
}
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abhishekroul.clientapp" >
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
在模拟器上连接但在Android手机上显示ETIMEDOUT错误。
答案 0 :(得分:0)
因此经过大量测试后,我通过使用端口80找到了解决问题的方法,HTTP协议通常永远不会被阻止,因为基于HTTP的进程将停止,因为此协议通常使用此端口,因此对用户更好端口80用于创建套接字。