我正在为Android构建一个应用程序,它将与PC通信并交换数据。我主要通过几个例子来学习如何制作应用程序以及如何使用Java套接字。我一直在尝试的一个例子是:
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html我从上面的代码修改了一些东西(变量和按键功能的名称),当我使用IP地址:10.0.2.2,并在模拟器中运行应用程序,并在连接到专用网络的桌面服务器上运行应用程序和服务器,应用程序和服务器工作。我可以从模拟器上的应用程序发送消息到服务器,服务器接收它。
然而,当我在笔记本电脑上运行服务器,在android手机上运行客户端应用程序,两者都无线连接到另一个专用网络时,我在android工作室控制台得到一个错误,说"没有路由到主机。"我进入我的笔记本电脑的命令终端,并能够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();
}
}
大家好,我得到了我朋友的路由器,这是思科的Linksys E1000无线n路由器,并测试了我的android应用程序和Java服务器,它工作。我不知道具体的配置是什么,但我是这样做的:
-
在路由器上启用DHCP,否则你将不得不在你的PC和Android手机上设置静态IP地址
-
确保你的电脑和安卓手机在网络上被发现。我刚想起来在我的电脑上当我连接到这个路由器时,它问我是否希望能够在网络上被发现
-
关闭防火墙(和防病毒),或者保持端口打开你想让你的android应用程序的所有连接通过。
如果您仍然有问题,这里有一些通用的故障排除提示(因为您使用的设备会有所不同):
-
如果您没有为您的PC或android手机分配IP地址,并且启用了DHCP,请尝试重置路由器(将其关闭并再次打开;
-
对于windows,打开命令提示符并输入
ipconfig
并找到您的IPv4地址。一定要把地址打对。然后在命令提示符中输入ping 'android phone's IP Address'
例如:萍192.168.1.121
如果你收到回复,说明你的手机在网络上。
- Android手机,下载此应用程序:https://play.google.com/store/apps/details?id=ua.com.streamsoft.pingtools&hl=en
然后尝试ping您为ipconfig
找到的IPv4地址,如果您的手机可以ping它,那么您都很好。如果你不能ping通它,试着关闭防火墙和杀毒软件。