gpt4 book ai didi

Java双向套接字连接(服务器/客户端)

转载 作者:搜寻专家 更新时间:2023-11-01 03:10:30 25 4
gpt4 key购买 nike

我正在尝试做的是将一些 JSON 从 Android 手机发送到 Java 服务器,这工作正常。 Android/客户端看起来像这样:

                    Socket s = new Socket("192.168.0.36", 12390);
s.setSoTimeout(1500);

JSONObject json = new JSONObject();
json.put("emergency", false);
json.put("imei", imei);
json.put("lat", l.getLatitude());
json.put("lon", l.getLongitude());
json.put("acc", l.getAccuracy());
json.put("time", l.getTime());


BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
out.write(json.toString());
out.flush();
s.close();

服务器端是这样的:

        try {
s = new ServerSocket(port);
}
catch (IOException e) {
System.out.println("Could not listen on port: " + port);
System.exit(-1);
}


Socket c = null;
while (true) {
try {
c = s.accept();
} catch (IOException e) {
System.out.println("Accept failed: " + port);
System.exit(-1);
}
try {

BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine = null;
String result = "";
while ((inputLine = in.readLine()) != null) {
result = result.concat(inputLine);
}
System.out.println(result);

正如我所说,所有这些都有效。现在我想在收到来自客户端的消息后将消息从服​​务器发送回客户端。我这样扩展代码,Android/客户端:

                    Socket s = new Socket("192.168.0.36", 12390);
s.setSoTimeout(1500);

JSONObject json = new JSONObject();
json.put("emergency", false);
json.put("imei", imei);
json.put("lat", l.getLatitude());
json.put("lon", l.getLongitude());
json.put("acc", l.getAccuracy());
json.put("time", l.getTime());


BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
out.write(json.toString());
out.flush();

String inputLine = null;
String result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while ((inputLine = in.readLine()) != null) {
Log.d(TAG, in.readLine());
result = result.concat(inputLine);
}

服务器端:

        try {
s = new ServerSocket(port);
}
catch (IOException e) {
System.out.println("Could not listen on port: " + port);
System.exit(-1);
}


Socket c = null;
while (true) {
try {
c = s.accept();
} catch (IOException e) {
System.out.println("Accept failed: " + port);
System.exit(-1);
}
try {

BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine = null;
String result = "";
while ((inputLine = in.readLine()) != null) {
result = result.concat(inputLine);
}
System.out.println(result);

PrintWriter out = new PrintWriter(c.getOutputStream());
out.write("Hello phone");
out.flush();
out.close();

在客户端,没有任何东西进来,它卡在

                while ((inputLine = in.readLine()) != null) {
Log.d(TAG, in.readLine());
result = result.concat(inputLine);
}

直到套接字超时(永远不会进入循环)。我认为这可能是一个时间问题,例如服务器过早发出回复,因此客户端从未收到任何东西,但我试图把 out.write("Hello phone");几乎在代码的任何地方,总是相同的结果。会不会是从 ServerSocket 获取的 socket 无法发送数据?我在这里错过了什么,这让我困扰了一整天......

编辑:在 Nikolais 回答后,我尝试了这个(客户端):

                    out.write(json.toString());
out.newLine();
out.write("###");
out.flush();


String inputLine = null;
String result = "";
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("###")) {
break;
}
Log.d(TAG, in.readLine());
result = result.concat(inputLine);

}


s.close();

和服务器:

                while ((inputLine = in.readLine()) != null) {

result = result.concat(inputLine);
if (inputLine.contains("###")) {
System.out.println("received ###");
out.println("Hello phone");
out.println("###");
out.flush();
break;
}

}

想法是在客户端关闭套接字之前从服务器发送消息。仍然不起作用...有任何提示吗?

最佳答案

在服务器端,您永远无法发送“您好电话”。直到客户端关闭套接字,但此时它是无用的。这是因为 in.readLine() 阻塞,直到数据可用或 EOF,即套接字关闭。

您需要一种摆脱阅读循环的方法 - 发明(或采用)一些应用程序级协议(protocol),告诉您整个消息已收到。常用选项有定长消息、长度前缀、定界等。

关于Java双向套接字连接(服务器/客户端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11653481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com