网络是一种痛苦。在使用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 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;
}
}
我的“服务器”只是笔记本电脑上运行的python脚本:
import socket
HOST = '192.168.###.#' #(numbers omitted from S/O question)
PORT = 10000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if data is not None:
print data
conn.close()
以下服务器可以与在同一台笔记本电脑上运行的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连接(Starbucks)上进行了测试,并尝试了其他一些端口(80、4444、4445、5000)。
我在Android应用中收到的评论错误是:
java.net.ConnectException: failed to connect to /192.168.###.# (port 10000): connection failed: ETIMEDOUT (Connection timed out)
感谢您的关注,我们很乐意提供更多信息/运行更多测试以解决此问题。我也有兴趣考虑其他解决方案,以通过互联网将方向数据从手机发送到笔记本电脑。
最佳答案
噢,男孩,这是愚蠢和尴尬的。基本上,我使用了错误的IP地址(192.168 ....)。
在Windows中,使用ipconfig(Linux:ifconfig),然后选择与无线LAN(wlan)相对应的IP地址。我正在使用与虚拟机或废话相对应的另一个IP地址。
希望以后对其他人有帮助,我讨厌这些问题。
关于java - Android Socket无法连接到笔记本电脑服务器-但是本地Java程序可以,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17798521/