后台我正在尝试为我的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;
}
答案 0 :(得分:0)
如果服务器在您的PC上运行,请尝试输入您的PC IP地址,而不是serverip =“10.0.2.2”。