网络是一种痛苦。在使用 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连接(星巴克)上进行了测试,并尝试了其他几个端口(80、4444、4445、5000)。
我在我的 Android 应用程序中收到的评论错误是:
java.net.ConnectException: failed to connect to /192.168.###.# (port 10000): connection failed: ETIMEDOUT (Connection timed out)
感谢您查看,很乐意提供更多信息/运行更多测试以解决此问题。我也有兴趣考虑通过互联网将方向数据从手机发送到笔记本电脑的其他解决方案。