Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android套接字连接超时_Android_Sockets_Client Server_Connection Timeout - Fatal编程技术网

android套接字连接超时

android套接字连接超时,android,sockets,client-server,connection-timeout,Android,Sockets,Client Server,Connection Timeout,background我正在尝试为我的java服务器创建一个android客户端应用程序,并让它们使用TCP套接字进行通信 什么有效我的应用程序在android设备模拟器上运行时有效 什么不起作用当我在手机上运行应用程序时,在创建套接字时,连接超时 异常java.net.ConnectException:无法连接到/10.0.2.2(端口9111):连接失败:ETIMEDOUT(连接超时) 服务器和移动设备连接到同一个wifi网络,我已经关闭了笔记本电脑上的防火墙(我运行服务器的地方) 这可能是什

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

什么有效我的应用程序在android设备模拟器上运行时有效

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

异常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;
    }
公共类ConnectionHandler扩展异步任务{
公共静态最终字符串serverip=“10.0.2.2”;
公共静态最终int服务器端口=9111;
插座;
打印输出;
缓冲读取器;
@凌驾
受保护的字符串背景(字符串…行)
{
i(“AsyncTank”,“doInBackgoung:创建套接字”);
试一试{
s=新套接字(服务器IP、服务器端口);
i(“AsyncTank”、“doInBackgoung:Created Socket”);
}捕获(例外e){
Log.i(“AsyncTank”、“doInBackgoung:无法创建套接字”+e.toString());
}
如果(s.断开连接()){
试一试{
in=新的BufferedReader(新的InputStreamReader(s.getInputStream());
out=新的PrintWriter(s.getOutputStream(),true);
i(“AsyncTank”,“doInBackgoung:创建套接字,分配流”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
Log.i(“AsyncTank”,“doInBackgoung:无法分配流,套接字未连接”);
e、 printStackTrace();
}
}否则{
Log.i(“AsyncTank”,“doInBackgoung:无法分配流,套接字已关闭”);
}
用于(字符串行:行)
{
写流(行);
}
字符串结果=readFromStream();
返回结果;
}

如果服务器在您的电脑上运行,请尝试输入您的电脑IP地址,而不是serverip=“10.0.2.2”。

您是否检查了“服务器”电脑或任何路由器的防火墙规则?