简体   繁体   English

Telnet 测试正常,但我的 java socket 客户端不工作

[英]Telnet test is OK, but my java socket client is not working

My partner system services socket server.我的合作伙伴系统服务套接字服务器。 They don't share the client module, only test sample data.他们不共享客户端模块,只共享测试样本数据。
First, I tested using telnet program and it's successful.首先,我使用telnet程序进行了测试,它成功了。 this image link is the test using telnet program .这个图片链接是使用telnet程序的测试
But my Java socket client can't receive message from socket server.但是我的 Java 套接字客户端无法从套接字服务器接收消息。
this image link is result of my socket program.此图像链接是我的套接字程序的结果。
Would you help me?你能帮我吗? Thanks.谢谢。

    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();
    }
}

I found the solution.我找到了解决方案。

The sever socket program sent byte data, not string line.服务器套接字程序发送的是字节数据,而不是字符串行。 Please find below my source code.请在下面找到我的源代码。

Thanks everyone.谢谢大家。

        socket = new Socket(ip, port);
        System.out.println("SEND DATA[" +msg.length() + "] [" + msg + "]");
        oout = new BufferedOutputStream(socket.getOutputStream());
        iin = new BufferedInputStream(socket.getInputStream());
        oout.write(msg.getBytes());
        oout.flush();

        byte[] buffer = new byte[1156];
        System.out.println("RECV DATA");
        iin.read(buffer);

        System.out.println("RECV DATA [" + new String(buffer) +"]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM