Java双向套接字连接(服务器/客户端)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java双向套接字连接(服务器/客户端),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3788字,纯文字阅读大概需要6分钟。
内容图文
![Java双向套接字连接(服务器/客户端)](/upload/InfoBanner/zyjiaocheng/784/f3f4a2d706c04672bb2a5eba58b24de2.jpg)
我想要做的是将一些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获取的套接字有关,而不能发送数据吗?我在这里想念的是,这一整天都在困扰着我……
编辑:尼古拉斯回答后,我尝试了这个(客户端):
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;
}
}
想法是在客户端关闭套接字之前从服务器发送消息.仍然没有工作…任何提示?
解决方法:
在服务器端,您永远不会发送“Hello Phone”.直到客户端关闭套接字,但在那时它没用.这是因为in.readLine()阻塞,直到数据可用或EOF,即套接字关闭.
你需要一种摆脱阅读循环的方法 – 发明(或采用)一些应用程序级协议,告诉你收到整个消息.常见选项是固定长度消息,长度前缀,分隔等.
内容总结
以上是互联网集市为您收集整理的Java双向套接字连接(服务器/客户端)全部内容,希望文章能够帮你解决Java双向套接字连接(服务器/客户端)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。