Android Socket没有';无法连接到笔记本电脑服务器-但本地Java程序可以
网络是一种痛苦。在使用AsyncTasks的服务中,我尝试启动套接字通信:Android Socket没有';无法连接到笔记本电脑服务器-但本地Java程序可以,java,android,python,sockets,networking,Java,Android,Python,Sockets,Networking,网络是一种痛苦。在使用AsyncTasks的服务中,我尝试启动套接字通信: public class TransmitService extends Service { private Socket echoSocket = null; private static PrintWriter out = null; private String HOST = null; private int PORT = -1; private static Conte
public class TransmitService extends Service {
private Socket echoSocket = null;
private static PrintWriter out = null;
private String HOST = null;
private int PORT = -1;
private static Context context;
public static boolean isConnected = false;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
context = getApplicationContext();
HOST = intent.getExtras().getString("HOST");
PORT = intent.getExtras().getInt("PORT");
Toast.makeText(context, "HOST/PORT: "+HOST+"/"+PORT, Toast.LENGTH_LONG).show();
new initNetworkTask().execute();
return Service.START_NOT_STICKY;
}
// Send the orientation data
public static void sendData(float f1, float f2, float f3) {
new sendDataTask().execute(f1, f2, f3);
}
static class sendDataTask extends AsyncTask<Float, Void, Void> {
@Override
protected Void doInBackground(Float... params) {
try {
JSONObject j = new JSONObject();
j.put("yaw", params[0]);
j.put("pitch", params[1]);
j.put("roll", params[2]);
String jString = j.toString();
out.println(jString);
} catch (Exception e) {
Log.e("sendDataTask", e.toString());
}
return null;
}
}
class initNetworkTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
echoSocket = new Socket(HOST, PORT);
out = new PrintWriter(echoSocket.getOutputStream(), true);
out.println("Welcome.");
isConnected = true;
} catch (Exception e) {
Log.e("initNetworkTask", e.toString());
isConnected = false;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO for communication return IBinder implementation
return null;
}
}
以下服务器与运行在同一台笔记本电脑上的Java客户机配合良好:
public class DataSender {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
//BufferedReader in = null;
try {
echoSocket = new Socket("192.168.###.#", 10000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
//in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput); //out.println - THIS IS HOW DATA IS SENT
//System.out.println("echo: " + in.readLine());
}
out.close();
//in.close();
stdIn.close();
echoSocket.close();
}
}
我真的很想在我的Android应用程序中实现这一点——我的目的是通过TransmitService.sendData(f1、f2、f3)
将方向数据连续传输到我的笔记本电脑
在测试方面:我关闭了windows防火墙,在同一个WiFi连接(星巴克)上进行了测试,并尝试了其他几个端口(804444455000)
我在Android应用程序中得到的评论错误是:
java.net.ConnectException:无法连接到/192.168.####.#(端口10000):连接失败:ETIMEDOUT(连接超时)
感谢您的关注,我们很乐意提供更多信息/运行更多测试以解决此问题。我也有兴趣考虑通过互联网将定向数据从手机发送到笔记本电脑的其他解决方案 哦,孩子,这太傻了,太尴尬了。基本上我使用了错误的IP地址(192.168…) 在Windows中,使用ipconfig(Linux:ifconfig),并选择与无线LAN(wlan)对应的IP地址。我使用的是另一个IP地址对应的虚拟机或一些垃圾
希望这对以后的其他人有帮助,我讨厌这种问题。你们有网络托管吗?如果你能把它放在云端的一个盒子里试试,它可能会提供一些见解(可能更好地通过Windows笔记本电脑登录!!)不,事实上,我的应用程序是用于电话到笔记本电脑的无线通信。不过,如果你知道我可以用Android做什么替代方案,我会感兴趣的。只是觉得有趣的是,你从我们这里漏掉了一个私有IP:D@Fred这可能表明我缺乏任何与网络相关的经验。有一次我的朋友说他偷了我的IP地址,我让他“把它还给我”……你使用的是私人(不可路由)IP地址;您的手机必须与笔记本电脑位于同一个专用网络上。该错误告诉您的手机无法连接到指定的IP地址,因为在同一本地网络上没有具有该IP的路由/设备
public class DataSender {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
//BufferedReader in = null;
try {
echoSocket = new Socket("192.168.###.#", 10000);
out = new PrintWriter(echoSocket.getOutputStream(), true);
//in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: taranis.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput); //out.println - THIS IS HOW DATA IS SENT
//System.out.println("echo: " + in.readLine());
}
out.close();
//in.close();
stdIn.close();
echoSocket.close();
}
}