【发布时间】:2018-03-30 06:02:06
【问题描述】:
我的合作伙伴系统服务套接字服务器。他们不共享客户端模块,只测试样本数据。
首先,我使用 telnet 程序进行了测试,它是成功的。
this image link is the test using telnet program.
但是我的 Java 套接字客户端无法接收来自套接字服务器的消息。
this image link is result of my socket program.
你能帮我吗?
谢谢。
public static void main(String[] args) {
System.out.println("file.encoding: " + System.getProperty("file.encoding"));
try {
Socket socket;
final String HOST = "xx.xx.xx.xxx"; // partner test server
final int PORT = 8994;
try {
socket = new Socket(HOST, PORT);
} catch (IOException ioe) {
System.out.println(">>>");
System.out.println(ioe.getMessage());
System.out.println(">>>");
throw ioe;
}
System.out.println("sending data:");
OutputStream os = socket.getOutputStream();
byte[] b = "xxxx52017082410332310000000020321 ONL00000 080081COP0045 3xxxxxxxxxxxxxxx/jOsBUe5I11C2mtP0j5tPSww==20170824103323 ".getBytes();
for (int i = 0; i < b.length; i++) {
os.write(b[i]);
}
os.flush();
socket.shutdownOutput();
System.out.println(new String(b) + "[END]");
System.out.println("receiving data");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
socket.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
【问题讨论】:
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建 minimal reproducible example。使用“编辑”链接改进您的问题 - 不要通过 cmets 添加更多信息。谢谢!
-
“不工作”不是工作问题描述。你就像一个人称他的车库为“我的车发出奇怪的声音。现在告诉我如何修理”。不可能。
-
"但是我的 Java 套接字客户端不工作。"我们需要更多细节。 究竟是什么不起作用?您是如何尝试解决的?代码在哪里?
-
@GhostCat - “我的车发出奇怪的声音”的解决方案是正确拼写 cat :-)。薛定谔的猫:自 1935 年以来在盒子里思考 :-)
标签: java sockets character-encoding telnet