[英]java: How to read data coming to the particular socket port?
我有一個GPS設備正在將GPS數據發送到我的機器端口,但是我不確定哪種方法是讀取來自特定插槽的數據的最佳方法。
請幫助我完成此任務。 謝謝你,祝你有美好的一天。
試試這個從套接字讀取寫入
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis", 7);
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);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
對於一個好的網絡應用程序框架,您可以查看Apache Mina(http://mina.apache.org/),我們正在使用Mina從各種GPS設備接收和發送數據。 數據以ASCII格式輸入嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.