我已经为gps设备配置了所有基本配置设置。 此外,我已将服务器IP设置为我的Windows服务器端口号。 7700已成功完成。
在服务器端,我使用Java Socket监听端口7700
try (ServerSocket serverSocket = new ServerSocket(7700)) {
System.out.println("Server is listening on port 7700");
int loop_counter = 1;
while (true) {
System.out.println("--- Refresh : "+loop_counter+" ---");
Socket socket = serverSocket.accept();
System.out.println("New client connected");
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
String text;
String userInput;
while ((userInput = reader.readLine()) != null){
writer.println(userInput);
writer.flush();
//writer.close();
System.out.println("echo: " + userInput);
}
//socket.close();
loop_counter++;
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
输出是这样的:
Server is listening on port 7700
--- Refresh : 1 ---
New client connected
echo: xx
echo: V‚01
Server exception: socket closed
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at com.nru.test.Server.main(Server.java:29)
请允许任何人帮助我获得在服务器内获取GPS设备数据的正确方法。 谢谢
答案 0 :(得分:1)
以下是我们用于为服务器连接设置套接字的工作Java代码:
payNow( ) {
var options = {
"key": "rzp_test_dveDexCQKoGszl",
"amount":((this.buyNow==1)?this.shared.totalAmountWithDisocuntBuyNow:this.shared.totalAmountWithDisocunt)*100, // 2000 paise = INR 20
"name": " MARKET",
"description": "Order #",
"handler": function (response){
console.log(response);
alert(response.razorpay_payment_id);
},
"prefill": {
"name": this.shared.orderDetails.billing_firstname + " " + this.shared.orderDetails.billing_lastname,
"email": this.shared.customerData.customers_email_address,
"contact": this.shared.customerData.customers_telephone,
},
"notes": { },
"theme": {
"color": "blue"
}
};
var rzp1 = new Razorpay(options);
rzp1.open();
// body...
}
paid()
{alert();}
我们尝试使用Java代码使其无缝工作。如您所见,我们在开始实际连接之前设置public class SocketConnect {
private ServerSocket serverSocket = null;
private Socket socket = null;
private ObjectOutputStream sOutput;
private boolean bindSocketPerformed = false;
public boolean startSocket() {
final int port = 4321;
InetAddress addr = null;
try {
addr = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e) {
LOG.error("SocketConnection: UnknownHostException InetAddress.getByName ", e);
shutdownSocket(serverSocket, socket);
return false;
}
if (!bindSocketPerformed) {
try {
serverSocket = new ServerSocket();
} catch (IOException e) {
LOG.info("SocketConnection: IOException new ServerSocket ", e);
shutdownSocket(serverSocket, socket);
return false;
}
try {
serverSocket.setReuseAddress(true);
} catch (SocketException e) {
LOG.error("SocketConnection: IOException setReuseAddress/setSoTimeout ", e);
shutdownSocket(serverSocket, socket);
return false;
}
try {
SocketAddress socketAddress = new InetSocketAddress(addr, port);
serverSocket.bind(socketAddress);
bindSocketPerformed = true;
} catch (IOException e) {
LOG.error("SocketConnection: IOException bind ServerSocket ", e);
shutdownSocket(serverSocket, socket);
return false;
}
}
try {
socket = serverSocket.accept();
LOG.info("SocketConnection: Server Accept done");
LOG.info("Initializing with empty message");
LOG.debug("Finished writing and closing the first object");
} catch (IOException e) {
LOG.error("SocketConnection: IOException bind ServerSocket ", e);
shutdownSocket(serverSocket, socket);
return false;
}
return true;
}
private void shutdownSocket(ServerSocket serverSocket, Socket socket) {
try {
if (sOutput != null) {
sOutput.close();
}
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
LOG.info("SocketConnection: IOException socket.close ", e);
}
}
public writeMsg(MySharedObject obj) {
try {
LOG.debug("Before socket write");
sOutput = new ObjectOutputStream(socket.getOutputStream());
sOutput.writeObject(obj);
sOutput.flush();
LOG.info("After socket write");
}
catch (IOException e) {
LOG.error("Error sending message [{}]", obj.toString());
}
}
private void close() {
// try to close the connection
try {
if (sOutput != null)
sOutput.close();
} catch (Exception e) {
LOG.info("Close Input/output Streams exception", e);
}
try {
if (socket != null)
socket.close();
} catch (Exception e) {
LOG.info("Close Input/output Socket exception", e);
}
}
}
。
您可以使用您选择的任何可访问地址替换默认的Inet地址字符串“127.0.0.1”。端口号serverSocket.setReuseAddress(true)
也可以更改。