2012-11-29 167 views
1

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

什么工作我的应用程序,当我在Android设备模拟器中运行它

什么行不通当我我的手机上运行的应用程序,我得到连接超时创建套接字时的作品。

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

在服务器和移动设备被连接到同一wifi网络,我已经关闭了防火墙,我的笔记本电脑(其中i运行服务器)

可能是什么这个原因?感谢任何人的帮助

服务器

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 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; 
    } 
+0

您是否检查过“服务器”计算机或任何路由器的防火墙规则? –

回答

0

相反的SERVERIP =主要活动

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"); 
    } 
} 
} 

异步活动 “10.0.2.2” 尝试输入您的PC的IP地址,如果服务器在您的PC上运行。