【发布时间】:2016-11-09 02:09:01
【问题描述】:
我正在为 Android 构建一个应用程序,它将与 PC 通信并交换数据。我主要通过几个示例来学习如何制作应用程序以及如何使用 Java 套接字。我一直在努力工作的一个例子是:
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
我已经从上面的代码中修改了一些东西(变量名称和按键功能),当我使用 IP 地址:10.0.2.2,并在模拟器中运行应用程序和连接到桌面的服务器时专用网络,应用程序和服务器工作。我能够从模拟器上的应用程序向服务器发送消息,服务器接收它。
但是,当我在笔记本电脑上运行服务器并在 android 手机上运行客户端应用程序时,它们都以无线方式连接到另一个专用网络,我在 android studio 控制台中收到错误消息“No route to host强>。”我进入我的笔记本电脑命令终端并能够ping我的手机。我已经用正确的 IPv4 地址和套接字替换了代码区域。我还将我的笔记本电脑和手机连接到另一个无线网络,但仍然无法建立连接。
可能是什么问题?我必须在代码中指定什么东西才能启用连接吗?还是我必须对网络或硬件设备做一些事情来建立连接?
请注意:
活动、清单和布局的代码与链接中显示的基本相同,除了变量名称更改和我删除了函数:public boolean onCreateOptionsMenu(Menu menu)
感谢您的帮助!
编辑 2 下面是我的android客户端源代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.editText1); // reference to the text field
button = (Button) findViewById(R.id.button1); // reference to the send button
// Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = textField.getText().toString(); // get the text message on the text field
textField.setText(""); // Reset the text field to blank
SendMessage sendMessageTask = new SendMessage();
sendMessageTask.execute();
}
});
}
private class SendMessage extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
client = new Socket("192.168.1.37", 4444); // connect to the server
printwriter = new PrintWriter(client.getOutputStream(), true);
printwriter.write(messsage); // write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); // closing the connection
} catch (UnknownHostException e) {
Context context = getApplicationContext();
/* If I try to send a toast notification my app crashes
String text = "Could not connect";
int duration = Toast.LENGTH_SHORT;
Toast notify = Toast.makeText(context, text, duration);
notify.show();*/
e.printStackTrace();
} catch (IOException e) {
Context context = getApplicationContext();
/*String text = "Could not connect";
int duration = Toast.LENGTH_SHORT;
Toast notify = Toast.makeText(context, text, duration);
notify.show();*/
e.printStackTrace();
}
return null;
}
}
}
服务器源代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author alexc
*/
public class JavaServer {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
public static void main(String[] args) throws IOException {
try {
serverSocket = new ServerSocket(4444); // Server socket
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}
System.out.println("Server started. Listening to the port 4444");
int count = 0;
while (count < 1000) {
count++;
try {
clientSocket = serverSocket.accept(); // accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); // get the client message
message = bufferedReader.readLine();
System.out.println(message);
inputStreamReader.close();
clientSocket.close();
} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}
serverSocket.close();
}
}
【问题讨论】:
-
你只需要更改IP地址。那你用了什么?
-
I get an error in the android studio console。安慰?我认为 logcat 中有一个例外。 -
@greenapps 是的,对于不正确的术语感到抱歉,但是是的,logcat 显示了一个异常,上面写着“没有路由到主机”。我习惯于调用 IDE 输出区域、控制台。是的,我在 android 客户端上的这行代码(android 客户端示例代码的第 57 行)中将 IP 地址更改为网络上笔记本电脑的 IP 地址:'client = new Socket(myIP, 4444);' myIP 是一个 IPv4 地址,例如 192.xxx.x.x.
-
调整您的代码,以便用户了解异常情况。然后切换防火墙。
-
你可能会安装一些安卓控制台(终端)应用程序并尝试 ping 到服务器 ip.. 检查你是否得到同样的错误
标签: java android sockets networking