Telnet测试没问题,但是我的java socket客户端无法运行

时间:2017-10-18 11:13:43

标签: java sockets character-encoding telnet

我的合作伙伴系统服务套接字服务器。他们不共享客户端模块,只测试样本数据 首先,我使用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();
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

服务器套接字程序发送字节数据,而不是字符串行。 请在下面找到我的源代码。

谢谢大家。

        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) +"]");