android 套接字连接超时

Posted

技术标签:

【中文标题】android 套接字连接超时【英文标题】:android socket connection timed out 【发布时间】:2012-11-17 01:18:11 【问题描述】:

背景我正在尝试为我的 java 服务器创建一个 android 客户端应用程序并让它们使用 TCP 套接字进行通信。

什么有效我的应用在安卓设备模拟器上运行时有效

什么不起作用当我在手机上运行该应用程序时,我在创建套接字时连接超时。

异常 java.net.ConnectException:无法连接到 /10.0.2.2(端口 9111):连接失败:ETIMEDOUT(连接超时)

服务器和移动设备连接到同一个 wifi 网络,我关闭了笔记本电脑上的防火墙(我运行服务器的地方)

这可能是什么原因?谢谢大家的帮助

服务器

public class HelpMeServer 
public final static int port=9111;
public static void main(String[] args) throws IOException 

    Database db=new Database();
    ServerSocket serverSocket = null;
    try 
        serverSocket = new ServerSocket(port);
        System.out.println("Listening on port "+port);
     catch (IOException e) 
        System.err.println("Could not listen on port: "+port);
        System.exit(1);
    

   while(true)
   
    Socket clientSocket = null;
    try 
        clientSocket = serverSocket.accept();
        ClientThread t=new ClientThread(clientSocket,db);
        t.start();
     catch (IOException e) 
        System.err.println("Accept failed.");
        System.exit(1);
    
   


主要活动

 public class LoginActivity extends Activity 

private EditText view_email;
private EditText view_password;
TextView result;
ConnectionHandler conhandler;
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    view_email=(EditText) findViewById(R.id.email);
    view_password=(EditText) findViewById(R.id.password);
    result=(TextView) findViewById(R.id.result);

public void login(View view)
    
    String email=view_email.getText().toString();
    String password=view_password.getText().toString();
    ConnectionHandler c=new ConnectionHandler();
    c.execute(new String[]"login",email,password);
    try 
        String response=c.get();
        if(response.equals("login successful"))
        
            Intent i=new Intent(this,MainActivity.class);
            startActivity(i);
        
        result.setText(response);
     catch (InterruptedException e) 
        result.setText("err");
        e.printStackTrace();
     catch (ExecutionException e) 
        e.printStackTrace();
        result.setText("err");
    


启动套接字的异步活动

public class ConnectionHandler extends AsyncTask<String, Void, String>

public static final String serverip = "10.0.2.2";
public static final int serverport = 9111;
Socket s;
PrintWriter out;
BufferedReader in;
@Override
protected String doInBackground(String... lines)

Log.i("AsyncTank", "doInBackgoung: Creating Socket");
try 
    s = new Socket(serverip, serverport);
        Log.i("AsyncTank", "doInBackgoung: Created Socket");
     catch (Exception e) 
    Log.i("AsyncTank", "doInBackgoung: Cannot create Socket "+e.toString());
    
        if (s.isConnected()) 
        try 
        in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        out = new PrintWriter(s.getOutputStream(), true);
        Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
         catch (IOException e) 
        // TODO Auto-generated catch block
     Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
                e.printStackTrace();
            
         else 
    Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
        
        for (String line:lines)
        
            writeToStream(line);
        
        String result=readFromStream();
        return result;
    

【问题讨论】:

您是否检查过“服务器”计算机或任何路由器的防火墙规则? 【参考方案1】:

如果服务器正在您的 PC 上运行,请尝试输入您的 PC IP 地址,而不是 serverip = "10.0.2.2"。

【讨论】:

以上是关于android 套接字连接超时的主要内容,如果未能解决你的问题,请参考以下文章

如何为默认的swagger android客户端设置连接和套接字超时

有没有办法区分连接超时和套接字超时?

Android socket.IO中的Emit或Ack超时处理?

如何配置套接字连接超时

如何配置套接字连接超时

Apache HttpClient 4.0 无法在 Android 上的套接字超时